-
Notifications
You must be signed in to change notification settings - Fork 3
Function Tracing
This page describes how to use Logme function tracing macros for logging function entry, function exit, arguments, return values, execution time and channel selection.
The examples below are based on the function tracing article and use the same API patterns.
Function tracing in logme is implemented through the LogmeP family of macros.
Naming rules:
-
Vmeans the function is traced without logging a return value - level suffixes:
-
D— Debug -
I— Info -
W— Warning -
E— Error -
C— Critical
-
If no level is specified, Info is used by default.
To log function entry and exit without parameters or return value, use:
LogmePV(); // or LogmePVI()#include <thread>
#include <chrono>
#include <Logme/Logme.h>
int MyProc(int i, const char* name)
{
LogmePV();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return i + 1;
}
int main()
{
MyProc(123, "abc");
return 0;
}Typical output:
>> MyProc()
<< MyProc()
To include execution time in the output, enable the Duration flag on the logging channel:
int main()
{
auto ch = Logme::Instance->GetDefaultChannelPtr();
auto flags = ch->GetFlags();
flags.Duration = true;
ch->SetFlags(flags);
MyProc(123, "abc");
return 0;
}Typical output:
>> MyProc()
<< MyProc() [102ms]
To log the return value, use a macro without the V suffix and pass a variable that stores the result:
int MyProc(int i, const char* name)
{
int ret{};
LogmePW(ret);
ret = i + 1;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return ret;
}Typical output:
>> MyProc()
<< MyProc() 124 [102ms]
To include function arguments in the log, pass them using helper macros.
int MyProc(int i, const char* name)
{
LogmePV(ARGS(i, name));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return i + 1;
}Typical output:
>> MyProc() 123, abc
<< MyProc()
int MyProc(int i, const char* name)
{
LogmePV(ARGS2(i, name));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return i + 1;
}Typical output:
>> MyProc() i=123, name=abc
<< MyProc()
Macros such as ARGS1, ARGS2, and similar variants control how many parameters are passed and allow logme to include both values and names.
For standard types, logme already provides built-in formatting. For user-defined types, define a formatter:
struct Point
{
int X;
int Y;
};
template<> std::string FormatValue<Point>(const Point& value)
{
return "Point(" + std::to_string(value.X) + ", " + std::to_string(value.Y) + ")";
}
static Point MakePoint(int x, int y)
{
Point r{ x, y };
LogmeP(r);
return r;
}This allows logme to print a meaningful representation of the custom type.
All previous examples use the default logging channel. To use a custom channel:
Logme::ID CHT{ "test" };
int MyProc(int i, const char* name)
{
int ret{};
LogmePI(ret, CHT, ARGS2(i, name));
ret = i + 1;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return ret;
}
int main()
{
auto ch = Logme::Instance->CreateChannel(CHT);
ch->AddLink(::CH);
MyProc(123, "abc");
return 0;
}This is useful when different subsystems or components need separate logging streams.
Typical output uses the following conventions:
- function entry:
>> Foo()
- function exit:
<< Foo()
- with execution time:
<< Foo() [102ms]
- with return value:
<< Foo() 124 [102ms]
- arguments without names:
>> Foo() 123, abc
- arguments with names:
>> Foo() i=123, name=abc
- Use the
LogmePmacro family for function tracing. - Use
LogmePVwhen no return value needs to be logged. - Use
LogmeP...withoutVwhen you want to log the return value through a result variable. - Enable the
Durationflag on the channel when execution time is needed. - Use
ARGS(...)for values andARGS2(...)for names and values.
logme — flexible runtime logging system
Home · Getting Started · Architecture · Output · Backends · Configuration
GitHub: https://github.com/efmsoft/logme
- Home
- Getting Started
- Why logme?
- Core Concepts
- Logging Macros
- Fatal Handling
- Crash Logging
- glog Compatibility
- C API
- Choosing Logging Macros
- Function tracing
- Trace Points
- Override Scopes
- Advanced Features
- Collapse Logging
- Feature Map
- Overview
- Console Backend
- Debugger Backend
- File Backend
- File Rotation & Retention
- Buffer Backend
- Ring Buffer Backend
- SharedFile Backend
- Callback Backend
- Windows Event Log Backend
- Custom Backends
- Runtime Control
- Configuration
- Configuration JSON
- Control Server
- Environment Control
- Control Policies
- Trace Points
- Message Filtering