-
Notifications
You must be signed in to change notification settings - Fork 3
/
output_log.cpp
executable file
·110 lines (94 loc) · 1.9 KB
/
output_log.cpp
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
/*
* log.cpp
*
* Created on: Nov 11, 2009
* Author: Adam Auton
* ($Revision: 66 $)
*/
#include "output_log.h"
output_log::output_log()
{
output_to_screen = true;
output_to_file = true;
}
void output_log::open(bool stout, bool sterr, const string &filename_prefix)
{
if (stout)
{
output_to_screen = false;
output_to_file = true;
}
if (sterr)
{
output_to_screen = true;
output_to_file = false;
}
if (output_to_file)
LOG.open((filename_prefix + ".log").c_str(), ios::out);
}
void output_log::close()
{
LOG.close();
}
void output_log::printLOG(string s)
{
if (output_to_file)
LOG << s; LOG.flush();
if (output_to_screen)
cerr << s; cerr.flush();
}
void output_log::error(string err_msg, int error_code)
{
printLOG("Error: " + err_msg + "\n");
exit(error_code);
}
void output_log::error(string err_msg, double value1, double value2, int error_code)
{
printLOG("Error: " + err_msg + "\n");
stringstream ss;
ss << "Value1=" << value1 << " Value2=" << value2 << endl;
printLOG(ss.str());
exit(error_code);
}
void output_log::warning(string err_msg)
{
printLOG(err_msg + "\n");
}
void output_log::one_off_warning(string err_msg)
{
static set<string> previous_warnings;
if (previous_warnings.find(err_msg) == previous_warnings.end())
{
printLOG(err_msg + "\n");
previous_warnings.insert(err_msg);
}
}
string output_log::int2str(int n)
{
std::ostringstream s2( std::stringstream::out );
s2 << n;
return s2.str();
}
string output_log::longint2str(long int n)
{
std::ostringstream s2( std::stringstream::out );
s2 << n;
return s2.str();
}
string output_log::dbl2str(double n, int prc)
{
std::ostringstream s2;
if ( prc > 0 )
s2.precision(prc);
s2 << n;
return s2.str();
}
string output_log::dbl2str_fixed(double n, int prc)
{
std::ostringstream s2;
s2 << setiosflags( ios::fixed );
if ( prc > 0 )
s2.precision(prc);
s2 << n;
return s2.str();
}