26
26
27
27
#include "diod_log.h"
28
28
29
- static char * prog = NULL ;
30
-
29
+ static char log_prefix [32 ] = { 0 };
31
30
static char * filename = NULL ;
32
31
static FILE * logf = NULL ;
33
32
33
+ /* If 'path' is a program path, prefix is "name: ", where name is basename (path).
34
+ * If 'path' ends in a space, omit the ": ", e.g. path of "# " is used verbatim.
35
+ * If 'path' is NULL, there is no log prefix.
36
+ */
34
37
void
35
- diod_log_init (char * p )
38
+ diod_log_init (char * path )
36
39
{
37
- prog = basename (p );
40
+ if (path ) {
41
+ char * cp = strrchr (path , '/' );
42
+ snprintf (log_prefix ,
43
+ sizeof (log_prefix ),
44
+ "%s%s" ,
45
+ cp ? cp + 1 : path ,
46
+ isspace (path [strlen (path ) - 1 ]) ? "" : ": " );
47
+ }
48
+ else
49
+ log_prefix [0 ] = '\0' ;
38
50
logf = stderr ;
39
51
}
40
52
@@ -93,7 +105,7 @@ _verr (int errnum, const char *fmt, va_list ap)
93
105
char * s = strerror_r (errnum , errbuf , sizeof (errbuf )); /* GNU version */
94
106
#endif
95
107
vsnprintf (buf , sizeof (buf ), fmt , ap ); /* ignore overflow */
96
- fprintf (logf , "%s: %s: %s\n" , prog , buf , s );
108
+ fprintf (logf , "%s%s: %s\n" , log_prefix , buf , s );
97
109
fflush (logf );
98
110
}
99
111
}
@@ -105,7 +117,7 @@ diod_log_msg (const char *fmt, va_list ap)
105
117
106
118
if (logf ) {
107
119
vsnprintf (buf , sizeof (buf ), fmt , ap ); /* ignore overflow */
108
- fprintf (logf , "%s: %s\n" , prog , buf );
120
+ fprintf (logf , "%s%s\n" , log_prefix , buf );
109
121
fflush (logf );
110
122
}
111
123
}
0 commit comments