Skip to content

Commit 10b104c

Browse files
committed
Created a small singleton for logging to file
1 parent 999ffab commit 10b104c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/LoggingSingleton.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by atticus on 1/14/16.
3+
//
4+
5+
#include <unistd.h>
6+
#include <string.h>
7+
#include <mutex>
8+
#include <iostream>
9+
#include "LoggingSingleton.h"
10+
11+
std::mutex mutex;
12+
13+
LoggingSingleton::LoggingSingleton()
14+
{
15+
std::cout << "Creating LoggingSingleton instance" <<std::endl;
16+
}
17+
18+
LoggingSingleton* LoggingSingleton::GetInstance()
19+
{
20+
static LoggingSingleton pSingleton;
21+
return &pSingleton;
22+
}
23+
24+
void LoggingSingleton::log(int logFileDescriptor, const char* cFullMessage){
25+
mutex.lock();
26+
write(logFileDescriptor, cFullMessage, strlen(cFullMessage));
27+
mutex.unlock();
28+
}
29+

src/LoggingSingleton.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Created by atticus on 1/14/16.
3+
//
4+
5+
#ifndef HACKTTP_LOGGINGSINGLETON_H
6+
#define HACKTTP_LOGGINGSINGLETON_H
7+
8+
9+
class LoggingSingleton {
10+
public:
11+
static LoggingSingleton* GetInstance();
12+
void log(int logFileDescriptor, const char* cFullMessage);
13+
private:
14+
LoggingSingleton();
15+
static LoggingSingleton* pSingleton;
16+
};
17+
18+
19+
#endif //HACKTTP_LOGGINGSINGLETON_H

0 commit comments

Comments
 (0)