-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.hh
191 lines (162 loc) · 3.67 KB
/
log.hh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
log.hh - This file is part of MUSIC -
a code to generate multi-scale initial conditions
for cosmological simulations
Copyright (C) 2010 Oliver Hahn
*/
#ifndef __LOG_HH
#define __LOG_HH
#include <string>
#include <list>
#include <fstream>
#include <ctime>
#include <cstdarg>
#include <sstream>
/*!
* \brief System for logging runtime library errors, warnings, etc.
*
* This is the class that catches every (debug) info, warning, error, or user message and
* processes it. Messages can be written to files and/or forwarded to user function for
* processing messages.
*/
namespace MUSIC
{
class log
{
public:
log(){}
~log();
/*!
* \brief Types of logged messages.
*/
enum messageType
{
Info,
DebugInfo,
Warning,
Error,
FatalError,
User
};
/*!
* \brief Logged message of type MessageType with some info.
*/
struct message
{
messageType type;
std::string text;
tm when;
};
/*!
* \brief Open file where to log the messages.
*/
static void setOutput(const std::string& filename);
/*!
* \brief Get the filename of log.
*/
static const std::string& output() { return outputFile_; }
/*!
* \brief Add a new message to log.
* \param type Type of the new message.
* \param text Message.
* \remarks Message is directly passes to user reciever if one is set.
*/
static void send(messageType type, const std::string& text);
//static void send(messageType type, std::string& text);
/*!
* \brief Get the list of all of the logged messages.
*/
static const std::list<message>& messages() { return messages_; }
/*!
* \brief Get the last logged message.
*/
static const message& lastMessage() { return messages_.back(); }
/*!
* \brief Set user function to receive newly sent messages to logger.
*/
static void setUserReceiver(void (*userFunc)(const message&)) { receiver = userFunc; }
/*!
* \brief Set minimum level of message to be logged.
*/
static void setLevel(const log::messageType level);
private:
static std::string outputFile_;
static std::ofstream outputStream_;
static std::list<message> messages_;
static messageType logLevel_;
static void (*receiver)(const message&);
};
}
inline void LOGERR( const char* str, ... )
{
char out[1024];
va_list argptr;
va_start(argptr,str);
va_end(argptr);
vsprintf(out,str,argptr);
#pragma omp critical(log_message_send)
{
MUSIC::log::send(MUSIC::log::Error, std::string(out));
}
}
inline void LOGWARN( const char* str, ... )
{
char out[1024];
va_list argptr;
va_start(argptr,str);
va_end(argptr);
vsprintf(out,str,argptr);
#pragma omp critical(log_message_send)
{
MUSIC::log::send(MUSIC::log::Warning, std::string(out));
}
}
inline void LOGFATAL( const char* str, ... )
{
char out[1024];
va_list argptr;
va_start(argptr,str);
va_end(argptr);
vsprintf(out,str,argptr);
#pragma omp critical(log_message_send)
{
MUSIC::log::send(MUSIC::log::FatalError, std::string(out));
}
}
inline void LOGDEBUG( const char* str, ... )
{
char out[1024];
va_list argptr;
va_start(argptr,str);
va_end(argptr);
vsprintf(out,str,argptr);
#pragma omp critical(log_message_send)
{
MUSIC::log::send(MUSIC::log::DebugInfo, std::string(out));
}
}
inline void LOGUSER( const char* str, ... )
{
char out[1024];
va_list argptr;
va_start(argptr,str);
va_end(argptr);
vsprintf(out,str,argptr);
#pragma omp critical(log_message_send)
{
MUSIC::log::send(MUSIC::log::User, std::string(out));
}
}
inline void LOGINFO( const char* str, ... )
{
char out[1024];
va_list argptr;
va_start(argptr,str);
va_end(argptr);
vsprintf(out,str,argptr);
#pragma omp critical(log_message_send)
{
MUSIC::log::send(MUSIC::log::Info, std::string(out));
}
}
#endif //__LOG_HH