如何用C ++编写日志? [英] How to write log in C++?

查看:82
本文介绍了如何用C ++编写日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我在广播公司工作.公司制造编码器.我们已经为该过程板编写了代码.但是我们找不到导致错误的地方.所以我想写一个日志文件
所以请给我个主意,如何写日志文件.



谢谢
RAJ KISHOR YADAV

hi all


i am working in broadcasting company. company making encoder. we have written code for this proc board. but we are unable find to where its is giving to error. so i want to write a log file
so please give me idea how to write log file.



Thank you
RAJ KISHOR YADAV

推荐答案

最简单的日志记录功能是具有文件重定向的cerr.
CodeProject 中,您可以找到
The simplest logging feature is cerr with file redirection.
Here at CodeProject you may find a lot of articles about logging[^].


尝试以下代码:
Try this piece of code:
void __loggi(const TCHAR* t,...)
{
  const TCHAR*  file = __TEXT("c:\\temp\\mylogfile.txt");
  HANDLE        h = ::CreateFile(file,GENERIC_WRITE,0,0,OPEN_ALWAYS,0,0);
  if(INVALID_HANDLE_VALUE!=h)
  {
    unsigned long    w = 0;
    va_list          val;
    TCHAR            f[0x1000];
    int              l;

    va_start(val,t);
    l = _vsntprintf_s(f,sizeof(f)/sizeof(f[0]),_TRUNCATE,t,val);
    va_end(val);
    if(0==SetFilePointer(h,0,0,FILE_END))
    {
      if(sizeof(short)==sizeof(TCHAR))
      {
        unsigned short  unicode = 0xFeFF;
        WriteFile(h,(void*)&unicode,2,&w,0);
      }
    }
    WriteFile(h,(void*)f,l*sizeof(TCHAR),&w,0);
    CloseHandle(h);
  }
}


示例:


Example:

void main()
{
  __loggi(__TEXT("hello world\r\n"));
}



祝您好运.



Good luck.


MSDN:应用程序日志包含应用程序记录的特定事件.应用程序开发人员确定要监视的事件."
就您而言,您想在调试应用程序时编写错误日志.因此,您必须创建一些异常处理过程,并在文件中输出捕获的错误.对此使用 cerr clog 都没关系.使用 ostream 在文件中进行的任何输出都可以.重要的是如何隔离或捕获要记录的错误.
此处是用于创建异常处理并输出到 cerr (控制台)流的示例代码.
MSDN: "The application log contains specific events logged by applications. Applications developers decide which events to monitor."
In your case, you want to write error log while debugging your application. Thus, you have to create some exception handling procedure and output caught errors in file. It does not matter much if you use cerr or clog for this. Any output in file using ostream will be okay. What is important is to how to isolate or catch errors to be logged.
Here is a sample code to create exception handling with output to cerr (console) stream.


这篇关于如何用C ++编写日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆