Skip to content

Commit 5b0f17b

Browse files
dhirajfx3Pipe-Runner
authored andcommitted
Logger added (#54)
1 parent f2329e7 commit 5b0f17b

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set(
99
utils/thing
1010
utils/port_map
1111
utils/ip_map
12-
utils/influxdb_utils
12+
utils/log
1313
)
1414

1515
set(SRC_PATHS "")

src/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "udp_interface.h"
2525
#include "sensor_data_interface.h"
2626
#include "db_interface.h"
27+
#include "log.h"
2728

2829
using namespace std;
2930
using namespace boost::uuids;
@@ -43,6 +44,12 @@ void demo_user() {
4344
sensor_data_interface::SensorDI::Query("select * from device_data");
4445
sensor_data_interface::SensorDI::RecordDeviceData("d3:3e:af", "001d2s", "somedata");
4546
sensor_data_interface::SensorDI::GetDeviceData("d3:3e:af");
47+
48+
// Logger
49+
lattice_log::Log L("dummy.txt");
50+
L.INFO("something");
51+
L.ERROR("something terrible");
52+
L.SUCCESS("Threat eliminated");
4653
}
4754

4855
void StartUdpServer() {

src/utils/log/inc/log.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef LATTICE_LOG
2+
#define LATTICE_LOG
3+
#include <fstream>
4+
#include <string>
5+
6+
#define RED "\e[1;31m"
7+
#define BLUE "\e[1;34m"
8+
#define GREEN "\e[1;32m"
9+
#define YELLOW "\e[1;33m"
10+
#define END_COLOR "\e[0m"
11+
12+
using namespace std;
13+
14+
namespace lattice_log {
15+
16+
#define INFO(data) logger(BLUE, __FILE__, std::to_string(__LINE__), data)
17+
#define ERROR(data) logger(RED, __FILE__, std::to_string(__LINE__), data)
18+
#define SUCCESS(data) logger(GREEN, __FILE__, std::to_string(__LINE__), data)
19+
class Log {
20+
ofstream log_file_;
21+
bool timestamp_, file_name_, line_number_, std_out_;
22+
string prefix_;
23+
24+
public:
25+
Log(const string &log_file = "log_file.out"):
26+
timestamp_(1),
27+
file_name_(1),
28+
line_number_(1),
29+
std_out_(1),
30+
log_file_(log_file.c_str(), ios::app | ios::binary) {}
31+
32+
void logger(
33+
const string &color,
34+
const string &file_name,
35+
const string &line_number,
36+
const string &information);
37+
38+
~Log() { log_file_.close(); }
39+
};
40+
} // namespace lattice_log
41+
42+
#endif

src/utils/log/src/log.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
3+
#include <boost/date_time.hpp>
4+
5+
#include "log.h"
6+
7+
8+
using namespace std;
9+
10+
void lattice_log::Log::logger(
11+
const string &color,
12+
const string &file_name,
13+
const string &line_number,
14+
const string &data) {
15+
16+
string info = color;
17+
18+
if (timestamp_)
19+
info += to_iso_extended_string(boost::posix_time::second_clock::universal_time());
20+
21+
if (file_name_)
22+
info += "[ " + file_name + " ]";
23+
24+
if (line_number_)
25+
info += "[ " + line_number + " ] ";
26+
27+
info += data + END_COLOR + "\n";
28+
log_file_ << info;
29+
log_file_.flush();
30+
31+
if(std_out_)
32+
cout << info;
33+
}
34+

0 commit comments

Comments
 (0)