-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.h
125 lines (95 loc) · 2.64 KB
/
log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef _LOG_H_
#define _LOG_H_
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
#include "vec3.h"
#include "time.h"
namespace Renzoku {
// Custom endline character.
// This is a work around as currently endl does not work with Log stream.
const char endn = '\n';
struct ExitCode {
enum kExitCode {
EXIT_CODE_OK,
EXIT_CODE_LIBRARY_FAILED_TO_LOAD,
EXIT_CODE_SHADER_FAILED_TO_COMPILE,
EXIT_CODE_FILE_FAILED_TO_LOAD,
EXIT_CODE_SCENE_FILE_ERROR,
};
};
/**
* A wrapper of ostream that inserts a prefix string (DEBUG, INFO, ERROR, etc.)
* before every output.
*/
class LogStream {
public:
LogStream();
LogStream(ostream &out);
template <typename T>
friend LogStream& operator<< (LogStream& stream, const T &v) {
if (! stream.is_enable) return stream;
stream.out << v;
if (stream.has_file)
(*stream.file) << v;
return stream;
}
void set_enable(bool is_enable);
void set_file_stream(ofstream *file) {
this->file = file;
this->has_file = true;
}
protected:
ostream &out;
ofstream *file;
bool has_file;
bool is_enable;
};
/**
* A singleton class to collect statistics, errors and warnings from the program.
*/
class Log {
protected:
Log();
static Log *log;
public:
~Log();
static Log *instance();
static void open(string filename);
static void set_debug(bool is_debug);
void set_display_time(bool time);
/**
* Three levels of log:
* 1. Info: information for coding and testing.
* 2. Warn: notice needed. Error might occur, but program continues.
* 3. Error: error encountered. Program still can handle and continue.
* 4. Critical: error encountered and program exits immediately.
*/
static LogStream& info();
static LogStream& warn();
static LogStream& error();
static void critical(string s, ExitCode::kExitCode code);
static LogStream& debug();
protected:
void _open(string filename);
LogStream& get_info();
LogStream& get_warn();
LogStream& get_error();
LogStream& get_critical();
protected:
ofstream file;
LogStream *out_stream;
LogStream *err_stream;
LogStream *debug_stream;
protected:
inline string get_time_string();
bool display_time;
};
inline string Log::get_time_string() {
return display_time ? "[" + Time::get_system_time_string() + "] " : "";
}
// TODO: implement a chain to support output to both file and console (require decorator pattern or intercept pattern)
} // end namespace
#endif