CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    2

    Logging all passed Arguments to a Function

    I'm trying to write a macro (or something else) to log all the parameters passed to a function, but I'm really stuck here.

    In other words I want something like below:

    Code:
    void test(int a, double b, char c)
    {
      __LOG_FUNCTION__
    }
    
    void main()
    { 
      test(2,3,'c');
    }
    I need to write __LOG_FUNCTION__ in someway so the output would be something like:

    Code:
    Entering function test:
    arguments: a = 2, b = 3, c='c'
    is this possible in anyway?

  2. #2
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: Logging all passed Arguments to a Function

    Does the number or types of arguments change?

    If they do, or your _LOG_FUNCTION_ will be generic, you should search for: "perfect forwarding", it's from c++0x, so not many compilers will have this implemented, in VS 2010, as far as i remember, you can do it up to 10 arguments.

  3. #3
    Join Date
    May 2010
    Posts
    2

    Re: Logging all passed Arguments to a Function

    yeah, actually he ideal case that everything about the function is unknown.

    I've read about "perfect forwarding" but I can not understand what I can do with it here.
    more help plz?

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Logging all passed Arguments to a Function

    Well, do you really need something that generic? I mean, what are you going to log once the passed parameters are complicated objects like maps, that have no operator<< defined.

    Also, is it that important that your macro is __Log_func__ and not something like __log_func(args).

    I see two solutions, but I don't like the first one:
    1
    Code:
    #define LOGGER(func_name, argType1, arg1, ...) \
    cout << "entering " << func_name << etc
    
    #define LOGGED_FUNC(func_name, return_type, argType1, arg1, ...) \
    return_typ func_name(argType1 arg1, ... ) \
    {
        LOGGER(unc_name, argType1, arg1, ...)
    
    
    LOGGED_FUNC(foobar, void, int, a, double, b)
        //actual code
    }
    The second option: It requires some copy pasting of your arguments, but at least your code will look like something.
    void foobar(int a, double b)
    {
    LOGGER(foobar, int, a, dopuble b)
    //actual code
    }[/CODE]

    This is not exactly what you requested, but it should work. As for the variable amount of arguments. I'm sure copy pasta can solve your problem.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured