1

I am curious as to what is the most common and/or accepted way of logging debug print info for a c++ win32 application on Windows. I am not using visual studio, and am compiling with GCC.

I am used to developing on Android, and writing and monitoring logs using logcat. Is there something like this for win32?

EDIT:

Is it most common to use something like this?

https://msdn.microsoft.com/en-us/library/6xkxyz08.aspx

Scorb
  • 1,654
  • 13
  • 70
  • 144
  • Questions asking to recommend something are unfortunately offtopic... – Petr Oct 19 '15 at 18:45
  • How about [`std::clog`](http://en.cppreference.com/w/cpp/io/clog) ? For example, you can use [this technique](https://stackoverflow.com/a/10151286/865719) to redirect its output to a file. – maddouri Oct 19 '15 at 19:13
  • You wish to write simple text messages to a file? [Windows doesn't do that](https://msdn.microsoft.com/en-us/library/windows/desktop/aa385780.aspx). There are probably a million logger utilities that do though. I know of no dominant, white-backed logging package. – user4581301 Oct 19 '15 at 19:16
  • How can we tell what is most common? In the embedded systems world, we write to a file or output to a debug port. Embedded systems are common, so this would be a common thing to do. Another method is to write the log to non-volatile memory, then dump the non-volatile memory after the application has finished. – Thomas Matthews Oct 19 '15 at 19:31
  • Ok. So have patience with my naivety here. There is no standard logging method on windows like there is on android? So I should just write my own code to log to file, or use a third party api? – Scorb Oct 19 '15 at 19:33
  • In GUI / Windowing land, you could open up another edit window and send your logging text to that window. – Thomas Matthews Oct 19 '15 at 19:33
  • I don't understand what "open up another edit window" means. Sorry. :S – Scorb Oct 19 '15 at 19:34
  • In a GUI system, you can create windows. Your program would have one window. You would create another window to contain your log information. An edit window is a type of window that is simple and allows basic editing as well as saving to file; see NotePad. – Thomas Matthews Oct 19 '15 at 19:36
  • Voting to close - primarily opinion based. There is no standard for logging and the data that needs to be logged is application specific. In some cases, simple writing to a file will suffice. Others may need time stamps and thread IDs. For example, the product I'm working on has at least 3 logs, each one is different and records different information. – Thomas Matthews Oct 19 '15 at 19:39
  • @ThomasMatthews: There is a standard for logging built into Windows. It's called [Event Tracing for Windows](https://msdn.microsoft.com/en-us/library/windows/desktop/bb968803.aspx), and has been around for 15 years now. ETW supports provider-defined data. – IInspectable Oct 19 '15 at 21:32

2 Answers2

1

Although it does not provide the added functionality of filtering and daily / size rollover OutputDebugString is a good API that allows you to send debug logging messages.

The output can be retrieved and displayed with a special program, when the program is not started the output simply gets ignored.

Read more about it in this article: How to view output of OutputDebugString? (the same API call can be used from C++)

Depending on the size of the output you might want to use Eventlog of Windows or a logging framework like http://log4cpp.sourceforge.net/

Community
  • 1
  • 1
Marged
  • 10,577
  • 10
  • 57
  • 99
  • How do I use it? What library do I need to use it? – Scorb Oct 20 '15 at 00:25
  • OutputDebugString is a built-in API call to Windows, so no extra library is needed – Marged Oct 20 '15 at 06:16
  • It did not work for me. I had to include "windows.h" – Scorb Oct 20 '15 at 14:39
  • 1
    @ScottF: The documentation for [OutputDebugString](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362.aspx) explains, which header the function is declared in (*WinBase.h*), which header you are supposed to include (*Windows.h*), and which import library to add (*Kernel32.lib*). – IInspectable Oct 20 '15 at 14:46
  • @ScottF Windows.h is a header, not a library – Marged Oct 20 '15 at 14:52
1

The standard infrastructure for logging in Windows is Event Tracing. It is available (and used) in all parts of the OS, both by user mode applications and kernel mode modules:

Purpose
Event Tracing for Windows (ETW) provides application programmers the ability to start and stop event tracing sessions, instrument an application to provide trace events, and consume trace events. Trace events contain an event header and provider-defined data that describes the current state of an application or operation. You can use the events to debug an application and perform capacity and performance analysis.

Where applicable
Use ETW when you want to instrument your application, log user or kernel events to a log file, and consume events from a log file or in real time.

Developer audience
ETW is designed for C and C++ developers who write user-mode applications.

Run-time requirements
ETW is included in Microsoft Windows 2000 and later.

Community
  • 1
  • 1
IInspectable
  • 46,945
  • 8
  • 85
  • 181