1 /*
2 DSFML - The Simple and Fast Multimedia Library for D
3 
4 Copyright (c) 2013 - 2015 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 use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose, including commercial applications,
10 and to alter it and redistribute it freely, subject to the following restrictions:
11 
12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 
15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
16 
17 3. This notice may not be removed or altered from any source distribution
18 */
19 
20 ///A module containing the Error File output.
21 module dsfml.system.err;
22 
23 import std.stdio;
24 
25 /**
26 *Standard std.stdio.File instance used by DSFML to output warnings and errors.
27 *
28 *By default, err outputs to the same location as stderr, which is the console if there's one available.
29 *
30 *err can be redirected to write to another output, independantly of stderr, by using the open function. 
31 */
32 File err;
33 
34 static this()
35 {
36 	//Let's our err output go to the console by default
37 	err = stderr;
38 }
39 
40 unittest
41 {
42 	version(DSFML_Unittest_System)
43 	{
44 		import std.stdio;
45 		import std.file;
46 		
47 		writeln("Unit test for err");
48 	
49 		
50 		writeln("Writing a line to err");
51 		err.writeln("This line was written with err.");
52 
53 		writeln("Routing err to a file, and then writing to it.");
54 		err.open("log.txt", "w");
55 		err.writeln("This line was written with err after being routed to log.txt");
56 		err.detach();//need to detach before being able to read the contents of the file(it's in use while open)
57 
58 		writeln("Reading log.txt to confirm its contents.");
59 
60 		auto contents = cast(string)read("log.txt");
61 
62 		writeln("The contents of the text file are as follows: ", contents);
63 
64 		writeln("Routing err back to the console.");
65 		err = stderr;//in this case, stderr is still writing to the console, but I could have used stdout as well.
66 
67 		writeln("And writing to err one final time.");
68 		err.writeln("This is the last line in the unit test to be written to err!");
69 	
70 		writeln();
71 
72 	}
73 	
74 }