forked from Yasna1998/stackoverflow-in-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
59 lines (48 loc) · 1.21 KB
/
Logger.cpp
File metadata and controls
59 lines (48 loc) · 1.21 KB
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
//
// Created by navid-fkh on 11/21/18.
//
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
#include "Logger.h"
using namespace std;
Logger::Logger() {}
Logger& Logger::getInstance() {
static Logger lg;
return lg;
}
void Logger::log(std::string lg) {
FILE* datafil = fopen("datafile.txt","r");
if(!datafil){
FILE* datafile = fopen("datafile.txt","a");
fseek(datafile, 0, SEEK_SET);
fprintf(datafile,"%d", 1);
fclose(datafile);
}
FILE* datafile = fopen("datafile.txt","r");
int count;
fseek(datafile, 0, SEEK_SET);
fscanf(datafile,"%d",&count);
fclose(datafile);
FILE* datafile2 = fopen("datafile.txt","w");
char s[] = " ";
sprintf(s,"log.%d.txt",count);
fseek(datafile2, 0, SEEK_SET);
fprintf(datafile2,"%d", ++count);
fclose(datafile2);
FILE* logfile = fopen(s,"a");
fseek(logfile, 0, SEEK_SET);
const char *clg = lg.c_str();
fprintf(logfile,clg);
fclose(logfile);
this->logs.push_back(lg);
}
void Logger::printLogs() {
for (const auto &log : logs) {
std::cout << log << std::endl;
}
}
std::vector<std::string>& Logger::getLogs() {
return this->logs;
}