1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2017 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 /**
26  * By default, $(U err) outputs to the same location as `stderr`, which is the
27  * console if there's one available.
28  *
29  * It is a standard std.stdio.File instance, so it supports all the functions as
30  * defined by this structure (write, writeln, open, lock, etc.)
31  *
32  * $(U err) can be redirected to write to another output, independantly of
33  * `stderr`, by using the `open` function. Note that unlike SFML's `err()`,
34  * DSFML's `err` cannot be redirected to 'nothing' at this time.
35  *
36  * Example:
37  * ---
38  * // Redirect to a file
39  * auto file = File("dsfml-log.txt", "w");
40  * auto previous = err;
41  * err = file;
42  *
43  * // Restore the original output
44  * err = previous;
45  * ---
46  */
47 module dsfml.system.err;
48 
49 import std.stdio;
50 
51 /**
52 * Standard std.stdio.File instance used by DSFML to output warnings and errors.
53 */
54 File err;
55 
56 static this()
57 {
58 	//Let's our err output go to the console by default
59 	err = stderr;
60 }
61 
62 unittest
63 {
64 	version(DSFML_Unittest_System)
65 	{
66 		import std.stdio;
67 		import std.file;
68 
69 		writeln("Unit test for err");
70 
71 
72 		writeln("Writing a line to err");
73 		err.writeln("This line was written with err.");
74 
75 		writeln("Routing err to a file, and then writing to it.");
76 		err.open("log.txt", "w");
77 		err.writeln("This line was written with err after being routed to log.txt");
78 		err.detach();//need to detach before being able to read the contents of the file(it's in use while open)
79 
80 		writeln("Reading log.txt to confirm its contents.");
81 
82 		auto contents = cast(string)read("log.txt");
83 
84 		writeln("The contents of the text file are as follows: ", contents);
85 
86 		writeln("Routing err back to the console.");
87 		err = stderr;//in this case, stderr is still writing to the console, but I could have used stdout as well.
88 
89 		writeln("And writing to err one final time.");
90 		err.writeln("This is the last line in the unit test to be written to err!");
91 
92 		writeln();
93 
94 	}
95 
96 }