Journal Articles

CVu Journal Vol 11, #4 - Jun 1999 + Programming Topics
Browse in : All > Journals > CVu > 114 (20)
All > Topics > Programming (877)
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: A Software Probe

Author: Administrator

Date: 03 June 1999 13:15:31 +01:00 or Thu, 03 June 1999 13:15:31 +01:00

Summary: 

Body: 

Software doesn't always work first time. There are many ways of gaining insight into what's going on, and this is another. The software probe described here is simple, designed to be added by hand and removed when the debug is over - it could be left in place to be switched on by command or compile option as required - you code to extend.

The probe relies on linking a small function into your build and #including a header into source files under investigation. Thereafter, where some information is required a one-liner addition is all that is required.

Header (log.h):-

#ifndef LOG_H_
#define LOG_H_
extern void log(char const * text, char const * file, int const line);
extern char logbuf[];
#define LOG() log(logbuf,__FILE__, __LINE__)
#endif 
Source File (log.c) to link in:-
#include <stdio.h>
#include "log.h"
char const * logname = "DOS.LOG";
static FILE * fp = NULL;
static int usingLogFile = 1;
char logbuf[509]; /* max length defined in C Standard */
void log(char const * text, char const * file, int const line) {
  if(usingLogFile) {
    if( fp == NULL ) fp = fopen( logname, "w" );
    else  fp = fopen( logname, "a" );
    if( fp == NULL ) {
      usingLogFile = 0;
      printf( "### Unable to (re)open log file %s ###", logname );
    }
    else  {
      if( text != NULL && file != NULL ) fprintf( fp, "[%s:%d] %s\n", file, line, text );
      else if( text != NULL && file == NULL ) fprintf( fp, "%s\n", text );
      else if( text == NULL && file == NULL ) fprintf( fp, "<unexpected>\n" );
      else if( text == NULL && file != NULL ) fprintf( fp, "<NULL msg in [%s:%d]>\n", file, line );

      fclose( fp );
    }
  }
}

The usage in some file under test:-

#include "log.h"
#include <stdio.c>
...
/*..... possibly aberrant code ... */
sprintf(logbuf, "format spec", varaldic data you want to record); LOG();
/*...more code */

The probe can be placed in several files and as the probe labels the file and line number it is fairly straightforward to interpret the results collected in DOS.LOG (the filename used). The construction of log() ensures that the file always contains the last message logged as the file is closed each time - a God send when software crashes and you are trying to narrow in on the faulty code.

Notes: 

More fields may be available via dynamicdata ..