-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileGLogger.cpp
executable file
·99 lines (86 loc) · 3.12 KB
/
FileGLogger.cpp
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: FileGLogger.cpp
* Author: gedas
*
* Created on Pirmadienis, 2016, kovas 28, 22.32
*/
#include "FileGLogger.h"
GServer::FileGLogger::FileGLogger(std::string filePath, int debug) :
LocalGLogger(debug) {
// Nustatau pavadinima ir paveldejimo grandinele
this->className = this->className + ":FileGLogger";
// Pradine reiksme pranesumu failui
this->logFile = NULL;
// Bandau atverti faila
try {
this->openLogFile(filePath);
// Tirkinu ar pranesimu failas atvertas
if (!this->logFile->is_open()) {
GLogger::logError(this->className,
"Nepavyko atverti pranesimu failo");
}
} catch (int e) {
GLogger::logError(this->className, "Klaida atveriant pranesimu faila");
}
this->logDebug(this->className, "Objektas sukurtas");
}
GServer::FileGLogger::~FileGLogger() {
// Uzveriu pranesimu faila
GLogger::logDebug(this->className, "Uzveriu pranesuimu faila");
this->logFile->close();
GLogger::logDebug(this->className, "Naikinu pranesuimu failo objekta");
delete this->logFile;
GLogger::logDebug(this->className, "Objektas sunaikintas");
}
void GServer::FileGLogger::openLogFile(std::string filePath) {
using namespace std;
// Bandau atverti faila
try {
// Kuriu pranesimu failo objekta
this->logFile = new ofstream(filePath, std::ofstream::trunc);
} catch (int e) {
// Stringas saungantis kelia
std::string temp(filePath);
GLogger::logError(this->className, "Nepavyko atverti failo: " + temp);
GLogger::logError(this->className, "Klaida: " +
string(strerror(errno)));
}
}
void GServer::FileGLogger::logInfo(std::string className, std::string message) {
// Laikinas stringas
std::string tempString;
// Formuoju pranesimu eilute
this->formMessage(className, message, INFO, tempString);
// Skelbiu pranesimo eilute
this->logFile->write(tempString.c_str(), tempString.length());
// Irasai viska i pranesimu faila
this->logFile->flush();
}
void GServer::FileGLogger::logError(std::string className, std::string message) {
// Laikinas stringas
std::string tempString;
// Formuoju pranesimu eilute
this->formMessage(className, message, ERROR, tempString);
// Skelbiu pranesimo eilute
this->logFile->write(tempString.c_str(), tempString.length());
// Irasai viska i pranesimu faila
this->logFile->flush();
}
void GServer::FileGLogger::logDebug(std::string className, std::string message) {
// Tirkinu ar derinimo informacija ijungta
if (this->DEBUGlevel) {
// Laikinas stringas
std::string tempString;
// Formuoju pranesimu eilute
this->formMessage(className, message, INFO, tempString);
// Skelbiu pranesimo eilute
this->logFile->write(tempString.c_str(), tempString.length());
// Irasai viska i pranesimu faila
this->logFile->flush();
}
}