Back to index

lightning-sunbird  0.9+nobinonly
tracelog.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is lineterm.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Ramalingam Saravanan.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /* tracelog.c: Tracing/logging module implementation
00038  */
00039 
00040 /* public declarations */
00041 #include <stdlib.h>
00042 #include <stdarg.h>
00043 
00044 #include "unistring.h"
00045 #include "tracelog.h"
00046 
00047 #ifdef USE_NSPR_BASE
00048 #include "prlog.h"
00049 #endif
00050 
00051 /* private declarations */
00052 
00053 /* TRACELOG global variable structure */
00054 TlogGlobal tlogGlobal;
00055 
00056 static int initGlobal = 0;
00057 
00062 void tlog_init(FILE* fileStream)
00063 {
00064   int imodule;
00065 
00066 #ifdef DEBUG_LTERM
00067   fprintf(stderr, "tlog_init:\n");
00068 #endif
00069 
00070   /* Do not re-initialize */
00071   if (initGlobal)
00072     return;
00073 
00074   initGlobal = 1;
00075 
00076   /* Error output stream */
00077   tlogGlobal.errorStream = fileStream;
00078 
00079   /* Debugging is disabled initially */
00080   tlogGlobal.debugOn = 0;
00081 
00082   for (imodule=0; imodule<TLOG_MAXMODULES; imodule++) {
00083     tlogGlobal.messageLevel[imodule] = 0;
00084     tlogGlobal.functionList[imodule] = NULL;
00085   }
00086 
00087   return;
00088 }
00089 
00090 
00095 int tlog_set_level(int imodule, int messageLevel, const char *functionList)
00096 {
00097   int j;
00098 
00099 #ifdef DEBUG_LTERM
00100   fprintf(stderr, "tlog_set_level:%d, %d\n", imodule, messageLevel);
00101 #endif
00102 
00103   if ((imodule < 0) || (imodule >= TLOG_MAXMODULES))
00104     return -1;
00105 
00106   /* Message level */
00107   tlogGlobal.messageLevel[imodule] = messageLevel;
00108 
00109   /* Free function list string */
00110   free(tlogGlobal.functionList[imodule]);
00111 
00112   if (functionList == NULL) {
00113     tlogGlobal.functionList[imodule] = NULL;
00114 
00115   } else {
00116     /* Duplicate function list string */
00117     int slen = strlen(functionList);
00118     char *stem;
00119 
00120     if (slen > 1000) slen = 1000;
00121 
00122     stem = malloc((unsigned int) slen+3);
00123     strncpy(&stem[1], functionList, (unsigned int) slen);
00124     stem[0] = ':';
00125     stem[slen+1] = ':';
00126     stem[slen+2] = '\0';
00127 
00128     tlogGlobal.functionList[imodule] = stem;
00129 
00130     if (messageLevel > 0) {
00131       tlog_warning("tlog_set_level: module %d, functionList=\"%s\"\n",
00132                                     imodule, tlogGlobal.functionList[imodule]);
00133     }
00134   }
00135 
00136   /* Turn on debugging only if needed */
00137   tlogGlobal.debugOn = 0;
00138 
00139   if (tlogGlobal.errorStream != NULL) {
00140     for (j=0; j<TLOG_MAXMODULES; j++) {
00141       if ((tlogGlobal.messageLevel[j] > 0) ||
00142           (tlogGlobal.functionList != NULL))
00143         tlogGlobal.debugOn = 1;
00144     }
00145   }
00146 
00147   if (messageLevel > 0) {
00148     tlog_warning("tlog_set_level: module %d, messageLevel=%d\n",
00149                                     imodule, messageLevel);
00150   }
00151 
00152   return 0;
00153 }
00154 
00155 
00160 int tlog_test(int imodule, char *procstr, int level)
00161 {
00162   if (tlogGlobal.errorStream == NULL)
00163     return 0;
00164 
00165   if ((imodule < 0) || (imodule >= TLOG_MAXMODULES))
00166     return 0;
00167 
00168   if ( (level <= tlogGlobal.messageLevel[imodule]) ||
00169          ((tlogGlobal.functionList[imodule] != NULL) &&
00170           ( (strstr(tlogGlobal.functionList[imodule],procstr) != NULL) ||
00171              (strstr(procstr,tlogGlobal.functionList[imodule]) != NULL)) ) ) {
00172     /* Display message */
00173 #if defined(USE_NSPR_BASE) && !defined(DEBUG_LTERM)
00174     PR_LogPrint("%s%2d: ", procstr, level);
00175 #else
00176     fprintf(tlogGlobal.errorStream, "%s%2d: ", procstr, level);
00177 #endif
00178     return 1;
00179   }
00180 
00181   return 0;
00182 }
00183 
00184 
00186 void tlog_message(const char *format, ...)
00187 {
00188   va_list ap;             /* pointer to variable length argument list */
00189 
00190   if (tlogGlobal.errorStream == NULL)
00191     return;
00192 
00193   va_start(ap, format);   /* make ap point to first unnamed arg */
00194   vfprintf(tlogGlobal.errorStream, format, ap);
00195   va_end(ap);             /* clean up */
00196   return;
00197 }
00198 
00199 
00201 void tlog_warning(const char *format, ...)
00202 {
00203   va_list ap;             /* pointer to variable length argument list */
00204 
00205   if ((tlogGlobal.errorStream == NULL) || !tlogGlobal.debugOn)
00206     return;
00207 
00208   va_start(ap, format);   /* make ap point to first unnamed arg */
00209   vfprintf(tlogGlobal.errorStream, format, ap);
00210   va_end(ap);             /* clean up */
00211   return;
00212 }
00213 
00214 
00215 #ifdef _UNISTRING_H
00216 #define MAXCOL 1024            /* Maximum columns in line buffer */
00217 
00219 void tlog_unichar(const UNICHAR *buf, int count)
00220 {
00221   if (tlogGlobal.errorStream == NULL)
00222     return;
00223 
00224 #if defined(USE_NSPR_BASE) && !defined(DEBUG_LTERM)
00225   PR_LogPrint("U(%d):\n", count);
00226 #else
00227   fprintf(tlogGlobal.errorStream, "U(%d): ", count);
00228   ucsprint(tlogGlobal.errorStream, buf, count);
00229   fprintf(tlogGlobal.errorStream, "\n");
00230 #endif
00231 
00232 }
00233 #endif