C++ Log File Class
2013-05-24 17:03
141 查看
C++ can become very
difficult to use especially with large projects where you can't see
all the variables. Here's a very simple C++ logging class that can take
logs for your software
without any effort. This will make you a better programmer as you won't have to rely on
debuggers like in
Microsoft's Visual Studio.
First you should create a project, with files like log.h and log.cpp. Let's show you the log.h
file:
#include
<fstream>
using namespace std;
class Log {
public:
Log(char*
filename);
~Log();
void Write(char*
logline);
private:
ofstream m_stream;
};
I included fstream for
ofstream (which is an
output file stream).
The logging class is called Log, it has one private (non-accessible variable by
non-class-members) member called m_stream which will be the
file stream to write our
logs.
I have 3 functions, one constructor called Log, which takes a
char pointer input filename, and a destructor ~Log to close our
file and avoid memory leaks, and finally a void Write function
which will take a logline char pointer.
Here's the log.cpp file:
#include "log.h"
Log::Log(char*
filename) {
m_stream.open(filename);
}
void Log::Write(char*
logline){
m_stream << logline
<<
endl;
}
Log::~Log(){
m_stream.close();
}
I included the log.h, then defined the implementation for the 3
functions.
The constructor will open the file delivered by filename on our
stream.
The Write function will take a string and write it to the log
using << operator, and will add a new
line (endl) to the end of each call.
~Log the destructor will close the stream.
This really helps with debugging, so now I'll demonstrate how it
can be used in a project. Of course the code can be improved so
that you write less code in your main project.
#include "log.h"
#include
<cstdlib>
int main(){
Log
*pLog = new
Log("errors.log");
pLog->Write("Going into
our loop");
for(int i
= 0; i < 10; i++){
char c[50];
sprintf(c, "Looped:
%d times", i);
pLog->Write(c);
}
return
0;
}
I created a Log class pointer, with the parameter "errors.log"
which will be my error logs file.
I call Write to write a line of errors to my log.
If you need to include integers or other variable types, it may
be an idea to use sprintf to add the integer i to a char, which you
can then write to your log.
va_list, or variadic argument lists:
pLog->Write("Looped: %d
times!", i);
// no sprintf needed!
Using the following modification to Write function, you can
create a C++ variadic
function. This should allow you to avoid using sprintf every
time.
#include
<stdarg.h>
void Log::Write(const
char* logline, ...){
va_list
argList;
char
cbuffer[1024];
va_start(argList, logline);
vsnprintf(cbuffer, 1024, logline, argList);
va_end(argList);
m_stream << cbuffer
<<
endl;
}
You can also modify it further, by adding time stamps and I have
done this.
Add the time.h and program.
void
SDRLog::Write(const char* logline, ...)
{
va_list
argList;
char
cbuffer[1024];
va_start(argList,
logline);
vsnprintf(cbuffer, 1024,
logline, argList);
va_end(argList);
char
buff[100];
time_t now =
time(0);
struct tm
*sTm;
sTm =
localtime(&now);//gmtime(&now);
strftime(buff,
sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
m_stream
<< buff;
m_stream
<< ": ";
m_stream
<< cbuffer
<< endl;
}
now this code works well.
difficult to use especially with large projects where you can't see
all the variables. Here's a very simple C++ logging class that can take
logs for your software
without any effort. This will make you a better programmer as you won't have to rely on
debuggers like in
Microsoft's Visual Studio.
First you should create a project, with files like log.h and log.cpp. Let's show you the log.h
file:
#include
<fstream>
using namespace std;
class Log {
public:
Log(char*
filename);
~Log();
void Write(char*
logline);
private:
ofstream m_stream;
};
I included fstream for
ofstream (which is an
output file stream).
The logging class is called Log, it has one private (non-accessible variable by
non-class-members) member called m_stream which will be the
file stream to write our
logs.
I have 3 functions, one constructor called Log, which takes a
char pointer input filename, and a destructor ~Log to close our
file and avoid memory leaks, and finally a void Write function
which will take a logline char pointer.
Here's the log.cpp file:
#include "log.h"
Log::Log(char*
filename) {
m_stream.open(filename);
}
void Log::Write(char*
logline){
m_stream << logline
<<
endl;
}
Log::~Log(){
m_stream.close();
}
I included the log.h, then defined the implementation for the 3
functions.
The constructor will open the file delivered by filename on our
stream.
The Write function will take a string and write it to the log
using << operator, and will add a new
line (endl) to the end of each call.
~Log the destructor will close the stream.
This really helps with debugging, so now I'll demonstrate how it
can be used in a project. Of course the code can be improved so
that you write less code in your main project.
#include "log.h"
#include
<cstdlib>
int main(){
Log
*pLog = new
Log("errors.log");
pLog->Write("Going into
our loop");
for(int i
= 0; i < 10; i++){
char c[50];
sprintf(c, "Looped:
%d times", i);
pLog->Write(c);
}
return
0;
}
I created a Log class pointer, with the parameter "errors.log"
which will be my error logs file.
I call Write to write a line of errors to my log.
If you need to include integers or other variable types, it may
be an idea to use sprintf to add the integer i to a char, which you
can then write to your log.
Variadic Functions
If you want to avoid using sprintf, and just using Write withva_list, or variadic argument lists:
pLog->Write("Looped: %d
times!", i);
// no sprintf needed!
Using the following modification to Write function, you can
create a C++ variadic
function. This should allow you to avoid using sprintf every
time.
#include
<stdarg.h>
void Log::Write(const
char* logline, ...){
va_list
argList;
char
cbuffer[1024];
va_start(argList, logline);
vsnprintf(cbuffer, 1024, logline, argList);
va_end(argList);
m_stream << cbuffer
<<
endl;
}
You can also modify it further, by adding time stamps and I have
done this.
Add the time.h and program.
void
SDRLog::Write(const char* logline, ...)
{
va_list
argList;
char
cbuffer[1024];
va_start(argList,
logline);
vsnprintf(cbuffer, 1024,
logline, argList);
va_end(argList);
char
buff[100];
time_t now =
time(0);
struct tm
*sTm;
sTm =
localtime(&now);//gmtime(&now);
strftime(buff,
sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
m_stream
<< buff;
m_stream
<< ": ";
m_stream
<< cbuffer
<< endl;
}
now this code works well.
相关文章推荐
- C++ Log File Class
- codeblocks进行C++开发 向工程中添加Class 发生编译错误:"No such file or directory" 解决方案
- 在c++中&nbsp;struct&nbsp;和class
- eclipse启动出错:Could not 和An error has occurred.See the log file D:\workspace\.metadata\.log.
- 关于log file sync事件
- 自己的路 自己把握 C++ 、Java 、C…
- C++&nbsp;operator关键字(重载操作符)
- C++&nbsp;chapter&nbsp;1
- C/C++&nbsp;static&nbsp;相关细节
- Static Nested Class&nb…
- [Android]Gradle 插件 DiscardFilePlugin(class注入&清空类和方法)
- Problems during startup. Check the".log" file in "metadata" of your workspace.
- 关于头文件中的include&nbsp;和class引…
- C++ 多态的实现原理--虚函数表
- [原]eclipse 编译c/c++环境搭建问…
- Unable to open log device '/dev/log/main': No such file or directory
- Unable to open log device '/dev/log/main&': No such file or directory
- 【原创】js 预读 input file内容
- Linux&nbsp;下&nbsp;C++程序的异常处理技巧
- c++&nbsp;try&nbsp;catch&nbsp;throw&nbsp;用法