1 /* 2 * DSFML - The Simple and Fast Multimedia Library for D 3 * 4 * Copyright (c) 2013 - 2018 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 * 6 * This software is provided 'as-is', without any express or implied warranty. 7 * In no event will the authors be held liable for any damages arising from the 8 * use of this software. 9 * 10 * Permission is granted to anyone to use this software for any purpose, 11 * including commercial applications, and to alter it and redistribute it 12 * freely, subject to the following restrictions: 13 * 14 * 1. The origin of this software must not be misrepresented; you must not claim 15 * that you wrote the original software. If you use this software in a product, 16 * an acknowledgment in the product documentation would be appreciated but is 17 * not required. 18 * 19 * 2. Altered source versions must be plainly marked as such, and must not be 20 * misrepresented as being the original software. 21 * 22 * 3. This notice may not be removed or altered from any source distribution 23 * 24 * 25 * DSFML is based on SFML (Copyright Laurent Gomila) 26 */ 27 28 /** 29 * By default, $(U err) outputs to the same location as `stderr`, which is the 30 * console if there's one available. 31 * 32 * It is a standard std.stdio.File instance, so it supports all the functions as 33 * defined by this structure (write, writeln, open, lock, etc.) 34 * 35 * $(U err) can be redirected to write to another output, independantly of 36 * `stderr`, by using the `open` function. Note that unlike SFML's `err()`, 37 * DSFML's `err` cannot be redirected to 'nothing' at this time. 38 * 39 * Example: 40 * --- 41 * // Redirect to a file 42 * auto file = File("dsfml-log.txt", "w"); 43 * auto previous = err; 44 * err = file; 45 * 46 * // Restore the original output 47 * err = previous; 48 * --- 49 */ 50 module dsfml.system.err; 51 52 import std.stdio; 53 54 /** 55 * Standard std.stdio.File instance used by DSFML to output warnings and errors. 56 */ 57 File err; 58 59 static this() 60 { 61 //Let's our err output go to the console by default 62 err = stderr; 63 64 //redirect sf:err() 65 sfErr_redirect(&writeToErr); 66 } 67 68 //Redirect sf::err() to write to our err File instance 69 private extern(C) void sfErr_redirect(void function(const(char*) str, int size) writeFunc); 70 71 //export a function to be used in C++ so that SFML can write to err 72 private extern(C) void writeToErr(const(char*) str, int size) 73 { 74 err.write(str[0..size]); 75 } 76 77 unittest 78 { 79 version(DSFML_Unittest_System) 80 { 81 import std.stdio; 82 import std.file; 83 import dsfml.graphics.texture; 84 85 writeln("Unit test for err"); 86 87 writeln("Writing a line to err"); 88 err.writeln("This line was written with err."); 89 90 writeln("Routing err to a file, and then writing to it."); 91 err.open("log.txt", "w"); 92 93 err.writeln("This line was written with err after being routed to log.txt"); 94 95 auto noTexture = new Texture(); 96 97 //This should generate a message from SFML that get's written to out output file 98 noTexture.loadFromFile("nonexistantTexture1.png"); 99 noTexture.loadFromFile("nonexistantTexture2.png"); 100 101 102 err.detach();//need to detach before being able to read the contents of the file(it's in use while open) 103 104 writeln("Reading log.txt to confirm its contents."); 105 106 auto contents = cast(string)read("log.txt"); 107 108 writeln("The contents of the text file are as follows: ", contents); 109 110 writeln("Routing err back to the console."); 111 err = stderr;//in this case, stderr is still writing to the console, but I could have used stdout as well. 112 113 writeln("And writing to err one final time."); 114 err.writeln("This is the last line in the unit test to be written to err!"); 115 116 writeln(); 117 } 118 119 }