Best way to write data to log file

I have source code of dll library and i want to log data to a file that dll is passing between functions, for example a call:

EX1:
test.GetAdapter()->CreateSession(promise_++, foo::SessionType, foo::InitDataType::enc, reinterpret_cast<const uint8_t *>(PT.GetData()), PT.GetDataSize());

or function:
EX2:
void bar::GetCapabilities(const uint8_t* JW, uint32_t media, PWT::PWT_CAPS &caps){}

What is the best way to get and write every argument that is passed in EX1 and how to get and write to log file every argument that function receive in EX2, but keep in mind i don't know what type(int, float, string ...) the argument will be, is there a universal way i can write every type to file?


PS. I tried to code format but it won't open window to input code.
it isnt a window its just tags that tell it to use the format you pasted in rather than reformat it to message style.

is the log file text, can you just write it on a line, with a time stamp or something?
if every type can be used by cout / stream operator, it should be fine.

Something like this, perhaps (__VA_OPT__ requires C++20):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <functional>

struct mylog_
{
    template < typename T > mylog_& operator<< ( T&& v )
    {
        std::clog << '\t' << v << '\n' ;
        return *this ;
    }
}mylog;

template< typename CALLABLE, typename... ARGS >
decltype(auto) trace_invoke( CALLABLE fn, ARGS&&... args )
{
    std::clog << "args:\n" ;
    ( mylog << ... << args ) ;
    return std::invoke( std::forward<CALLABLE>(fn), std::forward<ARGS>(args)... ) ;
}

#define TRACE_INVOKE_FN( FN, ... ) \
    ( ( std::clog << "\nfunction:\n\t" << #FN << '\n' ), \
      trace_invoke( FN __VA_OPT__(,) __VA_ARGS__ ) )

#include <cstring>
#include <cstdlib>

int main()
{
    char cstr[100] = "HELLO+WORLD!" ;
    TRACE_INVOKE_FN( std::strcmp, cstr, "hello-world!" );
    TRACE_INVOKE_FN( std::strtok, cstr, "+-*/" );
    TRACE_INVOKE_FN( std::srand, 100 ) ;
}

http://coliru.stacked-crooked.com/a/6853447584a24d4c
Topic archived. No new replies allowed.