Skip to content

Commit c7f2a99

Browse files
committed
Reorganized some includes.
Fixed Logger to actually *use* LoggingSingleton. Fixed a couple of places to log using Logger instead of cout. Fixed typo in globals.h Fixed init order in server (Config, Logger and then SignalHandler) Made compiler happy by handling a couple of unhandled errors, thus currently no compiler warnings.
1 parent 10b104c commit c7f2a99

18 files changed

+169
-130
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ CMakeLists.txt
4646

4747
# Archive
4848
etap*.zip
49+
/Release/

src/BasicHTTP.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define SRC_BASICHTTP_H_
33

44
#include "DataHandler.h"
5-
#include "Exceptions.h"
65
#include "Logger.h"
76

87
// Currently returned HTTP CODES

src/Common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef SRC_COMMON_H_
2+
#define SRC_COMMON_H_
3+
4+
using namespace std;
5+
6+
// our exception model
7+
#include "Exceptions.h"
8+
9+
// commonly used libs
10+
#include <cerrno>
11+
#include <cstdlib>
12+
13+
#endif /* SRC_COMMON_H_ */

src/Config.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
#include <fstream>
2-
#include <sstream>
3-
#include <fcntl.h>
4-
#include <unistd.h>
5-
#include <string.h>
6-
#include <vector>
7-
#include <map>
8-
#include <signal.h>
91
#include "Config.h"
10-
#include "globals.h"
11-
#define CONFIG_SIZE 500
2+
#include "Logger.h"
123
/*
134
* config - loads config file on demand, keeps the config in shared RO memory for fast access
145
*/
156

167
std::map<string, string> configMap;
178

9+
Logger *logger;
1810
Config::Config() {
11+
queue_size = 10;
1912
loadConfigFileToMap();
20-
// printMapContents();
13+
logger = new Logger("Config");
14+
logger->debug("Ready");
2115
}
2216

2317

@@ -30,9 +24,16 @@ void Config::loadConfigFileToMap(){
3024
std::string key;
3125
size_t position;
3226

33-
int configFileDescriptor = open("./config", O_RDONLY);
27+
int configFileDescriptor;
28+
// TODO: Should take name of config file from constructor param (passed from CmdLine or some default)
29+
if ((configFileDescriptor = open("./config", O_RDONLY, S_IRUSR)) == -1) {
30+
throw ConfigException("Cannot open config file!");
31+
}
32+
3433
char configFileContents[CONFIG_SIZE];
35-
read(configFileDescriptor, &configFileContents, CONFIG_SIZE);
34+
if (read(configFileDescriptor, &configFileContents, CONFIG_SIZE) == -1) {
35+
throw ConfigException("Cannot read config file!");
36+
}
3637
close(configFileDescriptor);
3738

3839
//Removes everything after ; in config file
@@ -62,21 +63,22 @@ void Config::loadConfigFileToMap(){
6263
}
6364

6465
void Config::printMapContents(){
65-
cout << "Currently loaded Config:" << endl;
66+
logger->debug("Currently loaded Config:");
6667
for (const auto &p : configMap) {
67-
std::cout << "[" << p.first << "] = " << p.second << '\n';
68+
logger->debug("[" + p.first + "] = " + p.second);
6869
}
6970
}
7071

7172
Config::~Config() {
73+
delete(logger);
7274
}
7375

7476

7577
int Config::get_int_setting(std::string setting_name) {
76-
if(isSigusr1Recieved){
78+
if(isSigusr1Received){
7779
Config::loadConfigFileToMap();
78-
// Config::printMapContents();
79-
isSigusr1Recieved = false;
80+
Config::printMapContents();
81+
isSigusr1Received = false;
8082
}
8183
int returnInt = -1;
8284
try {
@@ -91,10 +93,10 @@ int Config::get_int_setting(std::string setting_name) {
9193

9294

9395
std::string Config::get_str_setting(std::string setting_name) {
94-
if(isSigusr1Recieved){
96+
if(isSigusr1Received){
9597
Config::loadConfigFileToMap();
9698
Config::printMapContents();
97-
isSigusr1Recieved = false;
99+
isSigusr1Received = false;
98100
}
99101
std::string returnString;
100102

src/Config.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
#ifndef SRC_CONFIG_H_
22
#define SRC_CONFIG_H_
3-
#include <iostream>
4-
#include <exception>
5-
#include <vector>
3+
4+
#include "Common.h"
5+
#include "globals.h"
6+
7+
#include <fcntl.h>
8+
#include <fstream>
9+
#include <map>
610
#include <signal.h>
11+
#include <sstream>
12+
#include <unistd.h>
13+
#include <vector>
714

15+
#define CONFIG_SIZE 500
816

917
class Config {
1018
private:
11-
// to be replaced with some dynamic map struct
12-
int queue_size = 10;
19+
int queue_size;
1320
static std::string configString;
1421
static std::string delimiter;
1522
static std::string configRelevantString;
@@ -26,19 +33,11 @@ class Config {
2633
static std::string get_str_setting(std::string);
2734
};
2835

29-
using namespace std;
30-
// Possibly we could also add a stack trace here:
31-
// http://stackoverflow.com/questions/353180/how-do-i-find-the-name-of-the-calling-function
32-
class ConfigException: public exception {
33-
std::string reason;
34-
35-
public:
36-
ConfigException(std::string msg = "Unknown exception") {
37-
reason = msg;
38-
}
39-
virtual const char* what() const throw() {
40-
return reason.c_str();
41-
}
36+
class ConfigException: public BaseException {
37+
public:
38+
ConfigException(std::string msg = "Unknown config exception") {
39+
this->reason = msg;
40+
}
4241
};
4342

4443
#endif /* SRC_CONFIG_H_ */

src/DataHandler/Exec.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#include "../DataHandler.h"
22

3-
#include <cstring>
4-
#include <fcntl.h>
5-
#include <unistd.h>
6-
#include <linux/limits.h>
7-
83
/*
94
* handlers - currently two are planned: static file handler, cgi script handler
105
* 1. cgi handler - fires given script and returns it's output
@@ -113,7 +108,14 @@ DataHandler::resource DataHandler::Exec::run_command(std::string args[], DataHan
113108

114109
// Now first write what we have
115110
if (data && data->type == "POST") {
116-
write(comms_in[PIPE_WRITE], data->data, data->size + 1);
111+
if (write(comms_in[PIPE_WRITE], data->data, data->size + 1) == -1 ) {
112+
char * err = std::strerror(errno);
113+
throw DataHandler::Exception(
114+
"Error while writing to child process: " + std::string(args[0]) +", "
115+
+ "error message: " + std::string(err ? err : "unknown error")
116+
);
117+
118+
}
117119
}
118120
close(comms_in[PIPE_WRITE]);
119121

src/DataHandler/Static.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
#include "../DataHandler.h"
22

3-
#include <cstring>
4-
#include <fcntl.h>
5-
#include <unistd.h>
6-
#include <linux/limits.h>
7-
8-
93
/*
104
* handlers - currently two are planned: static file handler, cgi script handler
115
* 1. cgi handler - fires given script and returns it's output
@@ -26,7 +20,6 @@ DataHandler::resource DataHandler::Static::get_file(std::string path) {
2620
this->logger->debug("Read file: " + path);
2721
int fd = open(path.c_str(), O_RDONLY);
2822
if (fd < 0) {
29-
// TODO: replace with a proper HTTP CODE
3023
char * err = std::strerror(errno);
3124
throw DataHandler::FileNotFound("Error while reading file contents at " + path + ": " + std::string(err ? err : "unknown error"));
3225
}
@@ -36,7 +29,10 @@ DataHandler::resource DataHandler::Static::get_file(std::string path) {
3629

3730
DataHandler::resource data;
3831
data.data = (char *) malloc(fsize+1);
39-
read(fd, data.data, fsize);
32+
if (read(fd, data.data, fsize) == -1) {
33+
char * err = std::strerror(errno);
34+
throw DataHandler::Exception("Error while reading file contents at " + path + ": " + std::string(err ? err : "unknown error"));
35+
}
4036
close(fd);
4137

4238
data.data[fsize] = '\0';

src/Exceptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class BaseException: public std::exception {
2222
virtual const char* what() const throw() {
2323
return (name() + ": " + reason).c_str();
2424
}
25+
virtual ~BaseException() throw() {};
2526
};
2627

2728
#endif /* SRC_EXCEPTIONS_H_ */

src/Logger.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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:
@@ -15,12 +14,10 @@
1514
*/
1615

1716
// global
18-
std::string log_path;
1917
std::time_t epochTimestamp;
20-
int logFileDescriptor;
21-
std::mutex mut;
2218

2319
Logger::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\nError: " + 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

4847
void 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
}

src/Logger.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef SRC_LOGGER_H_
22
#define SRC_LOGGER_H_
3-
#include <string>
43
#include "Config.h"
4+
5+
#include <cstring>
56
#define QUIET 0
67
#define WARNINGS 1
78
#define INFO 2
@@ -15,7 +16,9 @@ class Logger {
1516
std::string class_name;
1617
std::string fullMessage;
1718
std::string fullDateTimestamp;
18-
bool isLoggingToFileEnabled = true;
19+
std::string log_path;
20+
int logFileDescriptor;
21+
bool isLoggingToFileEnabled;
1922

2023
public:
2124
Logger() {}
@@ -28,6 +31,13 @@ class Logger {
2831
void warn(std::string msg) { this->_log(msg, WARNINGS); }
2932
void info(std::string msg) { this->_log(msg, INFO); }
3033
void debug(std::string msg) { this->_log(msg, DEBUG); }
34+
35+
class Exception: public BaseException {
36+
public:
37+
Exception(std::string msg = "Unknown logger exception") {
38+
this->reason = msg;
39+
}
40+
};
3141
};
3242

3343
#endif /* SRC_LOGGER_H_ */

0 commit comments

Comments
 (0)