-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cc
125 lines (98 loc) · 2.81 KB
/
log.cc
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
/*
log.cc - This file is part of MUSIC -
a code to generate multi-scale initial conditions
for cosmological simulations
Copyright (C) 2010 Oliver Hahn
*/
#include "log.hh"
#include <iostream>
#include <algorithm>
std::string RemoveMultipleWhiteSpaces( std::string s );
std::string MUSIC::log::outputFile_;
std::ofstream MUSIC::log::outputStream_;
std::list<MUSIC::log::message> MUSIC::log::messages_;
void (*MUSIC::log::receiver)(const message&) = NULL;
MUSIC::log::messageType MUSIC::log::logLevel_;
std::string RemoveMultipleWhiteSpaces( std::string s )
{
std::string search = " "; // this is 2 spaces
size_t index;
while( (index = s.find(search)) != std::string::npos )
{ // remove 1 character from the string at index
s.erase(index,1);
}
return s;
}
void MUSIC::log::send(messageType type, const std::string& text_)
//void MUSIC::log::send(messageType type, std::stringstream& textstr)
{
std::string text(text_);// = textstr.str();
// Skip logging if minimum level is higher
if (logLevel_)
if (type < logLevel_) return;
// log message
MUSIC::log::message m;
m.type = type;
m.text = text;
time_t t = time(NULL);
localtime_r(&t,&m.when);
messages_.push_back(m);
if( type==Info||type==Warning||type==Error||type==FatalError )
{
std::cout << " - ";
if(type==Warning)
std::cout << "WARNING: ";
if(type==Error)
std::cout << "ERROR: ";
if(type==FatalError)
std::cout << "FATAL: ";
std::cout << text << std::endl;
}
std::replace(text.begin(),text.end(),'\n',' ');
RemoveMultipleWhiteSpaces(text);
// if enabled logging to file
if(outputStream_.is_open())
{
// print time
char buffer[9];
strftime(buffer, 9, "%X", &m.when);
outputStream_ << buffer;
// print type
switch(type)
{
case Info: outputStream_ << " | info | "; break;
case DebugInfo: outputStream_ << " | debug | "; break;
case Warning: outputStream_ << " | warning | "; break;
case Error: outputStream_ << " | ERROR | "; break;
case FatalError:outputStream_ << " | FATAL | "; break;
case User: outputStream_ << " | info | "; break;
default: outputStream_ << " | ";
}
// print description
outputStream_ << text << std::endl;
}
// if user wants to catch messages, send it to him
if(receiver)
receiver(m);
}
void MUSIC::log::setOutput(const std::string& filename)
{
//logDebug("Setting output log file: " + filename);
outputFile_ = filename;
// close old one
if(outputStream_.is_open())
outputStream_.close();
// create file
outputStream_.open(filename.c_str());
if(!outputStream_.is_open())
LOGERR("Cannot create/open logfile \'%s\'.",filename.c_str());
}
void MUSIC::log::setLevel(const MUSIC::log::messageType level)
{
logLevel_ = level;
}
MUSIC::log::~log()
{
if(outputStream_.is_open())
outputStream_.close();
}