11#include " Logger.h"
2- #include " Config .h"
2+ #include " LoggingSingleton .h"
33#include < ctime>
44#include < fcntl.h>
5- #include < unistd.h>
6- #include < string.h>
75#include < map>
8- #include < mutex>
6+ #include < string.h>
7+ #include < unistd.h>
98
109/*
1110 * Simple logging utility with:
1514 */
1615
1716// global
18- std::string log_path;
1917std::time_t epochTimestamp;
20- int logFileDescriptor;
21- std::mutex mut;
2218
2319Logger::Logger (std::string path, std::string name) {
20+ isLoggingToFileEnabled = true ;
2421 std::map<string, int > map;
2522 map.insert (pair<string, int >(" QUIET" , 0 ));
2623 map.insert (pair<string, int >(" WARNINGS" , 1 ));
@@ -31,12 +28,14 @@ Logger::Logger(std::string path, std::string name) {
3128 this ->current_log_level = map.at (Config::get_str_setting (" current_log_level" ));
3229 this ->class_name = name;
3330 log_path = path;
34- logFileDescriptor = open (log_path.c_str (), O_WRONLY | O_APPEND | O_CREAT );
31+ logFileDescriptor = open (log_path.c_str (), O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
3532
3633 if (logFileDescriptor < 0 ){
3734 loggerStatus = " [" + this ->class_name + " ]" + " Error opening logfile at " +log_path+" , disabling logging to file\n Error: " + strerror (errno) + " \n " ;
3835 cLoggerStatus = loggerStatus.c_str ();
39- write (2 , cLoggerStatus, strlen (cLoggerStatus));
36+ if (write (STDERR_FILENO, cLoggerStatus, strlen (cLoggerStatus)) == -1 ) {
37+ throw Logger::Exception (" Cannot write to STDERR." );
38+ }
4039 isLoggingToFileEnabled = false ;
4140 }
4241}
@@ -47,23 +46,28 @@ Logger::~Logger() {
4746
4847void Logger::_log (std::string msg, int level) {
4948 if (this ->current_log_level >= level) {
50- mut.lock ();
5149 epochTimestamp = std::time (nullptr );
5250 fullDateTimestamp = std::asctime (std::localtime (&epochTimestamp));
53- fullMessage = " [" +
54- this ->class_name + " " +
55- fullDateTimestamp.substr (0 , fullDateTimestamp.size () - 1 ) + " ] " +
51+ fullMessage = " [" + fullDateTimestamp.substr (0 , fullDateTimestamp.size () - 1 ) + " ] " +
52+ " [:" + this ->class_name + " ] " +
5653 msg + " \n " ;
5754 if (!isLoggingToFileEnabled){
5855 fullMessage = " WARNING: Logging to file disabled " + fullMessage;
5956 }
6057
6158 const char * cFullMessage = fullMessage.c_str ();
6259
63- write (1 , cFullMessage, strlen (cFullMessage));
60+ // fetch a LoggingSingleton instance when it's actually needed
61+ LoggingSingleton *logWriter = LoggingSingleton::GetInstance ();
62+ logWriter->log (STDOUT_FILENO, cFullMessage);
6463 if (isLoggingToFileEnabled) {
65- write (logFileDescriptor, cFullMessage, strlen (cFullMessage));
64+ try {
65+ logWriter->log (logFileDescriptor, cFullMessage);
66+ }
67+ catch (LoggingSingleton::Exception &e) {
68+ const char * errMessage = std::string (" Error while writing to logfile: " + std::string (e.what ()) + " \n " ).c_str ();
69+ logWriter->log (STDOUT_FILENO, errMessage);
70+ }
6671 }
67- mut.unlock ();
6872 }
6973}
0 commit comments