-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
executable file
·182 lines (158 loc) · 4.37 KB
/
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
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
/*
The MIT License (MIT)
Copyright (c) 2017 Wassim Filali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
___________________________________________________________________________________
dependencies :
- bme_280 with datasheet's associated rights see bme280_server.hpp
___________________________________________________________________________________
start date : 19.02.2017
logger
*/
#include "log.hpp"
//for ios::out,... #issue once placed in the end of the includes, it does not recognise cout part of std::
#include <iostream>
#include <fstream>
#include <string>
//for getTime
#include "utils.hpp"
#include <stdio.h>
#include <string.h>
const int Log::loglevel_None = 0;
const int Log::loglevel_Error = 1;
const int Log::loglevel_Warning = 2;
const int Log::loglevel_Info = 3;
const int Log::loglevel_Debug = 4;
const int Log::loglevel_Verbose = 5;
std::ofstream Log::logfile;
bool Log::isLogFile = false;
bool Log::isLogOut = false;
bool Log::isReady = false;
int Log::level_file = Log::loglevel_None;
int Log::level_out = Log::loglevel_None;
std::stringstream Log::cout;
bool Log::config(json &conf)
{
bool res = true;
//logfile : log into a file------------------------------------------------------
if( conf.find("logfile") != conf.end() )
{
std::string fileName = conf["logfile"];
std::cout << "log : logfile = " << fileName << std::endl;
logfile.open(fileName.c_str(), (std::ios::out|std::ios::app) );
if(!logfile.is_open())
{
printf("could not open log file:%s\r\n",fileName.c_str());
}
else
{
if(conf.find("level_file") != conf.end() )
{
std::string lstr = conf["level_file"];
level_file = std::stoi(lstr);
std::cout << "log : level_file = " << level_file << std::endl;
}
else
{
level_file = loglevel_Warning;
}
}
}
//logout is on by default, only a 0 stops it------------------------------------
isLogOut = true;//by default
if(conf.find("level_out") != conf.end())
{
std::string lstr = conf["level_out"];
level_out = std::stoi(lstr);
std::cout << "log : level_out = " << level_out << std::endl;
}
else
{
level_out = loglevel_Debug;
}
isReady = res;
return isReady;
}
std::string logstr(int level)
{
switch(level)
{
case Log::loglevel_Error :
return "Error";
case Log::loglevel_Warning :
return "Warning";
case Log::loglevel_Info :
return "Info";
case Log::loglevel_Debug :
return "Debug";
case Log::loglevel_Verbose :
return "Verbose";
default:
return "";
}
}
void Log::log(const std::string &str,const int &level)
{
if (!isReady)
{
return;
}
std::string d = utl::getDay();
std::string t = utl::getTime();
//Debug 4 >= Info 3
if(level <= level_out)
{
std::cout << d << " " << t << "\t";
std::cout << logstr(level) << "\t" << str << std::endl;
}
if( (level <= level_file) && logfile.is_open())
{
logfile << d << " " << t << "\t";
logfile << logstr(level) << "\t"<< str << std::endl;
logfile.flush();
}
}
std::string Log::Error()
{
log(cout.str(),loglevel_Error);
cout.str(std::string());
return "";
}
std::string Log::Warning()
{
log(cout.str(),loglevel_Warning);
cout.str(std::string());
return "";
}
std::string Log::Info()
{
log(cout.str(),loglevel_Info);
cout.str(std::string());
return "";
}
std::string Log::Debug()
{
log(cout.str(),loglevel_Debug);
cout.str(std::string());
return "";
}
std::string Log::Verbose()
{
log(cout.str(),loglevel_Verbose);
cout.str(std::string());
return "";
}