-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
137 lines (110 loc) · 3.43 KB
/
Logger.cpp
File metadata and controls
137 lines (110 loc) · 3.43 KB
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
#include <iostream>
#include <unistd.h>
#include <ctime>
#include <cstring> // for strerror
#include <sys/socket.h>
#include <fcntl.h> // for fcntl
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include "./Logger.h"
const int SERVERPORT = 9898;
const char *IP = "127.0.0.1";
//TODO: set them 0 inside initializeLog
LOG_LEVEL logLevel = (LOG_LEVEL)10;
int flags = 0;
bool isWorking = true;
int socketFd = 0;
struct sockaddr_in serverAddr = {0};
pthread_t ptId = 0;
void check(int status, const std::string &error)
{
if (status == -1)
{
std::cerr << error << ":" << strerror(errno) << std::endl;
if (ptId > 0)
{
isWorking = false;
pthread_join(ptId, NULL);
}
if (socketFd > 0)
{
close(socketFd);
}
exit(EXIT_FAILURE);
}
}
void *recieveFunction(void *)
{
std::cout << "\nServer Thread: receiveFunction()\n";
ssize_t bytes_received;
LOG_LEVEL buffer;
socklen_t size = sizeof(serverAddr);
while (isWorking)
{
bytes_received = recvfrom(socketFd, &buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddr, &size);
// Warning: dont use the check function on recvfrom() as we dont want to exit the program.
if (bytes_received < 0)
{
// std::cout << "sleeping for 1 sec \n";
sleep(1);
}
else
{
std::cout << "Received: " << buffer << std::endl;
std::cout << "Updating the Log Level: " << buffer << std::endl;
SetLogLevel(buffer);
// sleep(1);
}
}
pthread_exit(EXIT_SUCCESS);
}
void InitializeLog()
{
// initialize:
//memset(&logLevel, 10, sizeof(logLevel));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(IP);
serverAddr.sin_port = htons(SERVERPORT);
check(socketFd = socket(AF_INET, SOCK_DGRAM, 0), "socket()");
// Make the socket non-blocking:
check(flags = fcntl(socketFd, F_GETFL, 0), "fcntl(get flags if any)");
check(fcntl(socketFd, F_SETFL, flags | O_NONBLOCK), "fcntl(set flags)");
// create a pthread to recieve data from the server.
if (pthread_create(&ptId, NULL, recieveFunction, NULL) != 0)
{
std::cerr << "pthread_create()" << strerror(errno) << std::endl;
}
//std::cout << "\nSevver: Waitng for server to connect.... \n";
}
void SetLogLevel(LOG_LEVEL _level)
{
logLevel = _level;
}
void Log(LOG_LEVEL _level, const std::string &_file, const std::string &_func, int _line, const std::string &_msg)
{
if (isWorking == true)
{
ssize_t bytes_sent;
std::string message = {0};
time_t now = time(0);
char *dt = ctime(&now);
if (_level >= logLevel)
{
//std::cout << "_level " << logLevel << std::endl;
// std::cout << "_server " << serverAddr.sin_port << std::endl;
message = std::string(dt) + " Level: " + std::to_string(_level) + ", File: " + _file + ", Function: " + _func + ", Line: " + std::to_string(_line) + ", Message: " + _msg;
check(bytes_sent = sendto(socketFd, message.c_str(), message.length(), 0, (struct sockaddr *)&serverAddr, sizeof(serverAddr)), "sendto()");
}
// else{
// std::cout << "Working \n";
// }
}
}
void ExitLog()
{
std::cout << "exiting\n";
isWorking = false;
pthread_join(ptId, NULL);
close(socketFd);
}