Skip to content

Commit bca216a

Browse files
author
lh5053
committed
Fixed log file creation.
Dropped LoggerBase (not needed anymore). Added Logger constructor with default from config (and restored simpler instantiation in Manager/Router/Worker). Added class name to our exception what() string Added basic exception logging/handling in main server (now the server terminates correctly, not by segfaulting).
1 parent 879d51d commit bca216a

File tree

7 files changed

+31
-41
lines changed

7 files changed

+31
-41
lines changed

src/Exceptions.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#ifndef SRC_EXCEPTIONS_H_
22
#define SRC_EXCEPTIONS_H_
33

4+
#include <cxxabi.h>
45
#include <exception>
56
#include <iostream>
7+
#include <typeinfo>
68

79
// Possibly we could also add a stack trace here:
810
// http://stackoverflow.com/questions/353180/how-do-i-find-the-name-of-the-calling-function
@@ -13,8 +15,12 @@ class BaseException: public std::exception {
1315
BaseException(std::string msg = "Unknown exception") {
1416
this->reason = msg;
1517
}
18+
std::string name() const {
19+
int status;
20+
return abi::__cxa_demangle(typeid(*this).name(), 0, 0, &status);
21+
}
1622
virtual const char* what() const throw() {
17-
return reason.c_str();
23+
return (name() + ": " + reason).c_str();
1824
}
1925
};
2026

src/Logger.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,7 @@ std::time_t epochTimestamp;
2020
int logFileDescriptor;
2121
std::mutex mut;
2222

23-
//Po co jest LoggerBase?
24-
25-
LoggerBase::LoggerBase(std::string path) {
26-
log_path = path;
27-
}
28-
29-
LoggerBase::~LoggerBase() {
30-
}
31-
32-
Logger::Logger(std::string name) {
33-
this->class_name = name;
34-
}
35-
36-
Logger::Logger(std::string path, std::string name) : LoggerBase(path) {
23+
Logger::Logger(std::string path, std::string name) {
3724
std::map<string, int > map;
3825
map.insert(pair<string, int>("QUIET", 0));
3926
map.insert(pair<string, int>("WARNINGS", 1));
@@ -43,17 +30,14 @@ Logger::Logger(std::string path, std::string name) : LoggerBase(path) {
4330
const char* cLoggerStatus;
4431
this->current_log_level = map.at(Config::get_str_setting("current_log_level"));
4532
this->class_name = name;
46-
logFileDescriptor = open(log_path.c_str(), O_WRONLY | O_APPEND);
33+
log_path = path;
34+
logFileDescriptor = open(log_path.c_str(), O_WRONLY | O_APPEND | O_CREAT );
4735

4836
if(logFileDescriptor < 0){
49-
loggerStatus = "[" + this->class_name + "]" + " Error opening logfile, disabling logging to file\nError: " + strerror(errno) + "\n";
37+
loggerStatus = "[" + this->class_name + "]" + " Error opening logfile at "+log_path+", disabling logging to file\nError: " + strerror(errno) + "\n";
5038
cLoggerStatus = loggerStatus.c_str();
5139
write(2, cLoggerStatus, strlen(cLoggerStatus));
5240
isLoggingToFileEnabled = false;
53-
} else {
54-
loggerStatus = "[" + this->class_name + "]" + " Logfile is open, logging to file enabled\n";
55-
cLoggerStatus = loggerStatus.c_str();
56-
write(1, cLoggerStatus, strlen(cLoggerStatus));
5741
}
5842
}
5943

src/Logger.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
#ifndef SRC_LOGGER_H_
22
#define SRC_LOGGER_H_
33
#include <string>
4+
#include "Config.h"
45
#define QUIET 0
56
#define WARNINGS 1
67
#define INFO 2
78
#define DEBUG 3
8-
//enum LogLevel { QUIET, WARNINGS, INFO, DEBUG };
99

10-
class LoggerBase {
10+
class Logger {
1111
protected:
12-
int current_log_level = DEBUG;
13-
public:
14-
LoggerBase() {}
15-
LoggerBase(std::string path);
16-
virtual ~LoggerBase();
17-
18-
virtual void _log(std::string msg, int level) {}
19-
virtual void info(std::string msg) {}
20-
};
12+
int current_log_level = DEBUG;
2113

22-
class Logger : public LoggerBase {
2314
private:
2415
std::string class_name;
2516
std::string fullMessage;
@@ -28,8 +19,9 @@ class Logger : public LoggerBase {
2819

2920
public:
3021
Logger() {}
31-
Logger(std::string name);
3222
Logger(std::string path, std::string name);
23+
// delegation is fun
24+
Logger(std::string name) : Logger(Config::get_str_setting("log_path"), name) {};
3325
virtual ~Logger();
3426

3527
void _log(std::string msg, int level);

src/Manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
pthread_mutex_t * mutex_pool; // separate locks for each thread
1818

1919
Manager::Manager() {
20-
this->logger = new Logger(Config::get_str_setting("log_path"), "Manager");
20+
this->logger = new Logger("Manager");
2121
this->worker_count = Config::get_int_setting("worker_count");
2222
this->pool = (pthread_t*) calloc(worker_count, sizeof(pthread_t));
2323
mutex_pool = (pthread_mutex_t*) calloc(worker_count, sizeof(pthread_mutex_t));

src/Router.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
Router::Router(int qsize, std::string port) {
2323
this->queue_size = qsize;
24-
this->logger = new Logger(Config::get_str_setting("log_path"), "Router");
24+
this->logger = new Logger("Router");
2525
this->port = port;
2626
// initialize the socket
2727
this->listening_socket_fd = this->init_socket();

src/Worker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
Worker::Worker(int socket_fd) {
23-
this->logger = new Logger(Config::get_str_setting("log_path"), "Worker");
23+
this->logger = new Logger("Worker");
2424
this->socket_fd = socket_fd;
2525
}
2626

src/server.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <iostream>
22
#include "CmdLine.h"
33
#include "Config.h"
4-
#include "Router.h"
54
#include "Logger.h"
5+
#include "Router.h"
66

77
/*
88
* "main" - should use other modules for everything, that is:
@@ -16,17 +16,25 @@ int main() {
1616
// Init basic classes
1717
CmdLine cmdline;
1818
Config config;
19-
Logger logger(Config::get_str_setting("log_path"), "Server");
19+
Logger logger("Server");
2020
//"hello from server.cpp" message, to be removed
2121
logger.info(config.get_str_setting("config_test"));
2222

2323
if(config.get_str_setting("start_full_server") == "true") {
2424
// starting the full server
2525
std::string port = config.get_str_setting("port");
26+
int queue_size = config.get_int_setting("queue_size");
2627
logger.info("Starting HackTTP at port: " + port);
2728

28-
Router router(config.get_int_setting("queue_size"), port);
29-
router.watch();
29+
try {
30+
Router router(queue_size, port);
31+
router.watch();
32+
}
33+
catch (BaseException &e) {
34+
logger.warn(
35+
std::string(e.what()) + ", terminating server..."
36+
);
37+
}
3038
} else {
3139
logger.info("Running in test mode");
3240
}

0 commit comments

Comments
 (0)