How to solve Variadic Arguments issue (using C)

From Try-AS/400
Revision as of 11:44, 28 November 2019 by Heiko (talk | contribs) (→‎The Issue)
Jump to navigation Jump to search

First of all, what are Variadic Arguments (VA)? It's a way to NOT define the number of arguments a function (or method) takes. You only define the args you definitely going to be given on every call. All additional arguments are then optional, but can be used if given.

While porting C-code from Linux to AS/400 I ran into a problem. This article is going to explain the issue and the solution.

The Issue

I[1] have some code that I have been using for years in macOS and Linux projects. It's purpose is to log messages with the source's filename, line number and some custom message.

This is how it's used in the code:

Say(0, "Error: Socket creation failed with %d\n", errNum);


This is how the define adds the filename and linenumber: /* Debugging internals, don't change! */

#if (kDebug)
   #define Say(logLevel, parameter, ...) logWithMethodName(__FUNCTION__, __LINE__, logLevel, (parameter), ##__VA_ARGS__);
#else
   #define Say(logLevel, parameter, ...) //#define KeinLoggingHeute
#endif

This is the actual logging code (you can skip that, it's not relevant, except for the parameters):

void	logWithMethodName(const char* methodName, int lineNumber, int logLevel, char *format, ...)
{
		if (gLogLevel>=logLevel)
		{
			/* Variables */
			char msg1[kMaxMsgLen];
			char msg2[kMaxMsgLen];
			char lMethodName[kMaxMethodLen];
			
			/* Get the arguments: https://jameshfisher.com/2016/11/23/c-varargs/ & https://unixpapa.com/incnote/variadic.html (!) & https://github.com/mpaland/printf/blob/master/printf.c (!) & https://stackoverflow.com/questions/21540778/pass-varargs-to-printf (!) */
			va_list argp;
			va_start(argp, format); // format being the last argument before '...'

			/* Combine format and arguments! */
#ifdef USEOWNIMPLEMENTATIONOFVSPRINTF
			memset(msg2, '\0', kMaxMsgLen);
			int msg2i=0;
			char tmp[kMaxMsgLen];
			char *p=NULL; //format;
			int	ci;
			for (ci=0; ci<strlen(format) && msg2i<kMaxMsgLen-1; ci++)
			{
				/* This works only for SOME formats, like %s or %d (NO PRECISION!)! Everything unknown will be treated as %d! */
				char c=format[ci];
				if (c=='%')
				{
					/* Uh! It's a parameter-dingens */
					char t=format[ci+1]; // get its type. NO SUPPORT FOR PRECISION YET!
					if (t=='s')
					{	/* It's a string */
						sprintf(tmp, "%s", va_arg(argp, char*));
						strcat(msg2, tmp);
						msg2i=strlen(msg2);
						ci++; // omit the "s"
					}
					else
					{	/* It's something else. Hopefully some kind of number */
						sprintf(tmp, "%d", va_arg(argp, int));
						strcat(msg2, tmp);
						msg2i=strlen(msg2);
						ci++; // omit the "s"
					}
				}
				else
				{
					/* Add everything else to the buffer */
					msg2[msg2i]=c;
					msg2i++;
				}
			}
#else
			vsprintf(msg2, format, argp);
#endif


				if (strlen(methodName) > kMaxMethodLen)
				{
					memcpy(lMethodName, methodName, kMaxMethodLen-1);
					lMethodName[kMaxMethodLen]='\0';
				}
				else
				{
					strcpy(lMethodName, methodName);
				}

				if (strlen(format) > kMaxFormatLen) format[kMaxFormatLen]='\0';
				
				sprintf(msg1, "%s (%d): ", lMethodName, lineNumber);
				strcat(msg1, msg2);

				/* Output to stdout */
				if (1)
				{
					printf(msg1);
				}


				/* Write to syslog */
				if (0)
				{
					//logger(msg1);
				}
			
			va_end (argp);
		}
	
	return;
}

The Solution