[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

logMsg Wrapper

I'm looking at a vendor provided logMsg function

int logMsg
    (
    char * fmt,  /* format string for print */
    int    arg1, /* first of six required args for fmt */
    int    arg2,
    int    arg3,
    int    arg4,
    int    arg5,
    int    arg6
    )

With the description:

This routine logs a specified message via the logging task. This routine's syntax is similar to printf( ) -- a format string is followed by arguments to format. However, the logMsg( ) routine requires a fixed number of arguments (6).

The first drawback is it requires 6 arguments after the format string -- even if the arguments aren't used.  This makes the function slightly annoying to use. To make it appear more printf-like, I suspect I could define seven macros which will take the form: logMsgN(fmt, arg1, arg2, ..., argn) where N indicates how many of the six trailing arguments there are.

Secondly, I'd like to wrap this logMsg function in a class.  An instance of that class will get created and I could do things like:

log << MSG::ERROR << "Unable to create address for  /dd" << '\n';
log << MSG::DEBUG << "Finalizing" << '\n';
log << MSG::WARNING
      << "whatever"
           << 5
      << '\n';

I suspect with the wrapper class I may not need the macros.  The number of arguments could be inferred via  "<<".  In any event, I'm open to ideas .. I'll increase points provide a solution exists with source code results (i.e show me source..if source doesn't warrant 60 points I'll increase accordingly)
0
forums_mp
Asked:
forums_mp
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
>> The first drawback is it requires 6 arguments after the format string
Just wrap it with your own function that has default parameters.


int logMsg
(
 char * fmt,  /* format string for print */
 int    arg1, /* first of six required args for fmt */
 int    arg2,
 int    arg3,
 int    arg4,
 int    arg5,
 int    arg6
 ) { return 0; }

int MylogMsg
(
 char * fmt,
 int    arg1 = 0,
 int    arg2 = 0,
 int    arg3 = 0,
 int    arg4 = 0,
 int    arg5 = 0,
 int    arg6 = 0
 ) { return logMsg(fmt, arg1, arg2, arg3, arg4, arg5, arg6); }


int main()
{
      MylogMsg("");
}

>> secondly, I'd like to wrap this logMsg function in a class.
See below


BTW: I'm a bit confused why it takes a format specification and yet all the params except for the first are type int!?

int logMsg
(
 char * fmt,  /* format string for print */
 int    arg1, /* first of six required args for fmt */
 int    arg2,
 int    arg3,
 int    arg4,
 int    arg5,
 int    arg6
 ) { return 0; }
 
int MylogMsg
(
 char * fmt,
 int    arg1 = 0,
 int    arg2 = 0,
 int    arg3 = 0,
 int    arg4 = 0,
 int    arg5 = 0,
 int    arg6 = 0
 ) { return logMsg(fmt, arg1, arg2, arg3, arg4, arg5, arg6); }
 
class Logger
{
public:
	Logger & operator << (int i) { MylogMsg("5d", i); return *this; }
};
 
 
int main()
{
	Logger logger;
	logger << 999;
}

Open in new window

0
 
Infinity08Commented:
Or, make use of the standard stdarg.h header :

        http://www.cplusplus.com/reference/clibrary/cstdarg/
int vlogMsg(char *fmt, va_list arg) {
  /* interpret the format string, and use va_arg to get the next argument from the va_list whenever needed */
}
 
int logMsg(char * fmt, ...) {   /* this accepts a variable amount of arguments */
  int ret = 0;
  va_list args;
  va_start(args, fmt);
  ret = vlogMsg(fmt, args);
  va_end(args);
  return ret;
}
 
/* call it : */
int result = logMsg("some format ...", 5, "test", 3.5);
int result = logMsg("some format ...");
int result = logMsg("some format ...", 5, "test", "test2", "test3", 3.5, 7.0, 23, 'a');
/* etc. */

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now