os-cmpi-xen

view src/cmpitrace.c @ 121:4868ace2726b

Add initial consistence checks in test suite for Xen_MemoryPool, Xen_ProcessorPool and Xen_VirtualSystemManagementService.

Signed-off-by: Luke Szymanski <Lukasz.Szymanski@Unisys.com>
author Jim Fehlig <jfehlig@novell.com>
date Fri Jun 08 10:22:05 2007 -0600 (2007-06-08)
parents fa1ca0490794
children
line source
1 // Copyright (C) 2006 IBM Corporation
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 // ============================================================================
17 // Author: Dr. Gareth S. Bestor <bestor@us.ibm.com>
18 // Contributors: Adrian Schuur <schuur@de.ibm.com>
19 // Summary: SBLIM_TRACE support for SBLIM Project CMPI providers.
20 // Description:
21 // TODO
22 // For more information about the SBLIM Project see:
23 // http://sblim.sourceforge.net/
24 // ============================================================================
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/time.h>
32 #include <time.h>
34 #include "cmpitrace.h"
36 /* Maximum length of trace message text. */
37 #define _MAXLENGTH 2048
39 /* By default log all trace messages. */
40 int _SBLIM_TRACE_LEVEL = _SBLIM_TRACE_LEVEL_ALL;
42 /* By default log trace messages to stderr. */
43 static char * _SBLIM_TRACE_FILE = NULL;
45 /* --------------------------------------------------------------------------*/
47 char * _sblim_format_trace(char * fmt, ...)
48 {
49 va_list ap;
50 char * msg = (char *)malloc(_MAXLENGTH);
51 va_start(ap, fmt);
52 vsnprintf(msg, _MAXLENGTH, fmt, ap);
53 va_end(ap);
54 return msg;
55 }
57 /* --------------------------------------------------------------------------*/
59 void _sblim_trace( int level, char * srcfile, int srcline, char * msg )
60 {
61 struct tm cttm;
62 struct timeval tv;
63 struct timezone tz;
64 long sec = 0;
65 char * tm = NULL;
66 FILE * ferr = stderr;
67 int pid;
69 static int firsttime = 1;
71 /* Initialize _SBLIM_TRACE_LEVEL and _SBLIM_TRACE_FILE from env vars. */
72 if (firsttime) {
73 char * tracelevel = getenv("SBLIM_TRACE");
74 if (tracelevel != NULL) _SBLIM_TRACE_LEVEL = atoi(tracelevel);
76 char * tracefile = getenv("SBLIM_TRACE_FILE");
77 if (tracefile != NULL) _SBLIM_TRACE_FILE = strdup(tracefile);
79 fprintf(ferr, "_SBLIM_TRACE_LEVEL=%x\n", _SBLIM_TRACE_LEVEL);
80 fprintf(ferr, "_SBLIM_TRACE_FILE=%s\n", _SBLIM_TRACE_FILE);
82 firsttime = 0;
83 }
85 /* Append this trace message to the existing _SBLIM_TRACE_FILE. */
86 if ((_SBLIM_TRACE_FILE != NULL) && (ferr = fopen(_SBLIM_TRACE_FILE, "a")) == NULL) {
87 fprintf(stderr, "Cannot open SBLIM_TRACE_FILE %s", _SBLIM_TRACE_FILE);
88 free(msg);
89 return;
90 }
92 /* Generate a timestamp for this trace message. */
93 if (gettimeofday(&tv, &tz) == 0) {
94 sec = tv.tv_sec + (tz.tz_minuteswest * -1 * 60);
95 tm = (char *) malloc(20 * sizeof(char));
96 memset(tm, 0, 20 * sizeof(char));
97 if (gmtime_r(&sec, &cttm) != NULL) strftime(tm, 20, "%m/%d/%Y %H:%M:%S", &cttm);
98 }
100 /* Get PID of the current process. */
101 pid = getpid();
103 /* Strip off the directory path from the compile-time src filename. */
104 if (index(srcfile,'/') != NULL) srcfile = index(srcfile,'/')+1;
106 char *level_str;
107 switch (level) {
108 case _SBLIM_TRACE_LEVEL_ERROR:
109 level_str = "ERROR";
110 break;
111 case _SBLIM_TRACE_LEVEL_WARNING:
112 level_str = "WARNING";
113 break;
114 case _SBLIM_TRACE_LEVEL_INFO:
115 level_str = "INFO";
116 break;
117 case _SBLIM_TRACE_LEVEL_DEBUG:
118 level_str = "DEBUG";
119 break;
120 default:
121 level_str = "DEBUG";
122 }
124 /* Format and print the trace message. */
125 fprintf(ferr, "[%s] [%s] %d --- %s(%i) : %s\n", level_str, tm, pid, srcfile, srcline, msg);
126 if (tm != NULL) free(tm);
128 if ((_SBLIM_TRACE_FILE != NULL)) fclose(ferr);
129 free(msg);
130 }