Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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
Posted
Updated 23-Aug-11 2:01am
v2

The simplest logging feature is cerr with file redirection.
Here at CodeProject you may find a lot of articles about logging[^].
 
Share this answer
 
Comments
fjdiewornncalwe 23-Aug-11 8:17am    
+5. The best answer on this one.
Try this piece of code:
C++
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.
 
Share this answer
 
Comments
Chandrasekharan P 24-Aug-11 0:30am    
Appreciate your effort. But a simple fprinf will not work here?
mbue 24-Aug-11 7:11am    
This should be nearly the same. But im afraid fprintf will not distinguish between ansi and unicode.
Regards.

[in addition]
This function is written so you can use it by a debug define (like trace):

#ifdef _DEBUG
#define LOG __loggi
#else
#define LOG
#endif

with a simple printf you cannot do that. On the other side you can extend the function inside to lock the access by mutex object (if necessary).
Chandrasekharan P 24-Aug-11 8:03am    
well my 5 for the explanation :)
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900