1818
1919#include " lib.error.h"
2020
21- #include <stdlib.h>
22- #include <string.h>
21+ #include < cstdlib>
22+ #include < cstring>
23+ #include < cstdarg>
24+ #include < cstdio>
25+ #include < ctime>
26+ #include < vector>
27+ #include < string>
28+ #include < iostream>
2329#include < unistd.h>
24- #include <time.h>
2530#include < sys/time.h>
2631
27- /* Define va_copy macro if not available (ANSI C99 only).
28- * memcpy() is fallback suggested by the autoconf manual, but doesn't
29- * work with g++ on AMD 64bit platform.
30- * FIXME: Replace by autoconf test?
31- */
32- #ifndef va_copy
33- #ifdef __va_copy
34- #define va_copy (dest , src ) __va_copy(dest,src)
35- #else
36- #define va_copy (dest , src ) memcpy(&dest, &src, sizeof(va_list))
37- #endif
38- #endif
39-
4032/* Use program name in syslogging if defined */
4133#ifndef PROGRAM
4234#define PROGRAM NULL
@@ -52,22 +44,28 @@ int loglevel = LOG_DEBUG;
5244FILE *stdlog = NULL ;
5345int syslog_open = 0 ;
5446
55- char * printf_escape (const char * str )
47+ /* Escape '%' characters in a string for use in printf like functions */
48+ static std::string escape_percent (const char *str)
5649{
57- char * escaped ;
58- char c ;
59- size_t str_pos , esc_pos ;
50+ if (str == NULL ) return " " ;
51+ std::string escaped;
52+ size_t len = strlen (str);
53+ size_t percent_count = 0 ;
54+
55+ for (size_t i = 0 ; i < len; ++i) {
56+ if (str[i] == ' %' ) {
57+ percent_count++;
58+ }
59+ }
6060
61- escaped = (char * )malloc (2 * strlen (str )+ 1 );
62- esc_pos = 0 ;
61+ escaped.reserve (len + percent_count);
6362
64- for (str_pos = 0 ; str_pos < strlen (str ); str_pos ++ ) {
65- c = str [str_pos ];
66- escaped [esc_pos ++ ] = c ;
67- if ( c == '%' ) escaped [esc_pos ++ ] = c ;
63+ for (size_t i = 0 ; i < len; ++i) {
64+ escaped += str[i];
65+ if (str[i] == ' %' ) {
66+ escaped += ' %' ;
67+ }
6868 }
69- escaped [esc_pos ] = 0 ;
70-
7169 return escaped;
7270}
7371
@@ -77,9 +75,7 @@ void vlogmsg(int msglevel, const char *mesg, va_list ap)
7775 struct timeval currtime;
7876 struct tm tm_buf;
7977 char timestring[128 ];
80- char * progname_escaped ;
81- char * buffer ;
82- int bufferlen ;
78+ std::string buffer;
8379 va_list aq;
8480 char *str, *endptr;
8581 int syslog_fac;
@@ -106,37 +102,36 @@ void vlogmsg(int msglevel, const char *mesg, va_list ap)
106102 strftime (timestring, sizeof (timestring), " %b %d %H:%M:%S" , &tm_buf);
107103 sprintf (timestring+strlen (timestring), " .%03d" , (int )(currtime.tv_usec /1000 ));
108104
109- progname_escaped = printf_escape (progname );
110- if ( progname_escaped == NULL ) abort ();
111-
112- bufferlen = strlen (timestring )+ strlen (progname_escaped )+ strlen (mesg )+ 20 ;
113- buffer = (char * )malloc (bufferlen );
114- if ( buffer == NULL ) abort ();
105+ std::string progname_escaped = escape_percent (progname);
115106
116- snprintf (buffer , bufferlen , "[%s] %s[%d]: %s\n" ,
117- timestring , progname_escaped , getpid (), mesg );
118-
119- free (progname_escaped );
107+ // Construct the format string: "[time] progname[pid]: message\n"
108+ buffer = " [" ;
109+ buffer += timestring;
110+ buffer += " ] " ;
111+ buffer += progname_escaped;
112+ buffer += " [" ;
113+ buffer += std::to_string (getpid ());
114+ buffer += " ]: " ;
115+ buffer += mesg;
116+ buffer += " \n " ;
120117
121118 if ( msglevel<=verbose ) {
122119 va_copy (aq, ap);
123- vfprintf (stderr , buffer , aq );
120+ vfprintf (stderr, buffer. c_str () , aq);
124121 fflush (stderr);
125122 va_end (aq);
126123 }
127124 if ( msglevel<=loglevel && stdlog!=NULL ) {
128125 va_copy (aq, ap);
129- vfprintf (stdlog , buffer , aq );
126+ vfprintf (stdlog, buffer. c_str () , aq);
130127 fflush (stdlog);
131128 va_end (aq);
132129 }
133130
134- free (buffer );
135-
136131 if ( msglevel<=loglevel && syslog_open ) {
137- buffer = vallocstr (mesg , ap );
138- syslog (msglevel , "%s" , buffer );
139- free (buffer );
132+ char *syslog_buf = vallocstr (mesg, ap);
133+ syslog (msglevel, " %s" , syslog_buf );
134+ free (syslog_buf );
140135 }
141136}
142137
@@ -161,47 +156,36 @@ void logmsg(int msglevel, const char *mesg, ...)
161156*/
162157char *errorstring (const char *type, int errnum, const char *mesg)
163158{
164- size_t buffersize ;
165- char * errtype , * errdescr , * buffer ;
159+ std::string errtype;
160+ std::string errdescr;
161+ std::string buffer;
166162
167163 /* Set errtype to given string or default to 'ERROR' */
168164 if ( type==NULL ) {
169- errtype = strdup (ERRSTR );
170- if ( errtype == NULL ) abort ();
165+ errtype = ERRSTR;
171166 } else {
172- errtype = ( char * ) type ;
167+ errtype = type;
173168 }
174169
175- errdescr = NULL ;
176170 if ( errnum != 0 ) {
177171 errdescr = strerror (errno);
178172 } else if ( mesg == NULL ) {
179- errdescr = strdup ( "unknown error" ) ;
173+ errdescr = " unknown error" ;
180174 }
181175
182- buffersize = strlen (errtype )
183- + (errdescr == NULL ? 0 : strlen (errdescr ))
184- + (mesg == NULL ? 0 : strlen (mesg ))
185- + 5 ;
186-
187- buffer = (char * )malloc (sizeof (char ) * buffersize );
188- if ( buffer == NULL ) abort ();
189- buffer [0 ] = '\0' ;
190-
191- strcat (buffer , errtype );
192- strcat (buffer , ": " );
176+ buffer = errtype + " : " ;
193177
194- if ( mesg != NULL ) strcat ( buffer , mesg ) ;
178+ if ( mesg != NULL ) buffer += mesg;
195179
196180 if ( mesg != NULL &&
197- errdescr != NULL ) strcat ( buffer , ": " ) ;
181+ ! errdescr. empty () ) buffer += " : " ;
198182
199- if ( errdescr != NULL ) strcat ( buffer , errdescr ) ;
183+ if ( ! errdescr. empty () ) buffer += errdescr;
200184
201- if ( type == NULL ) free ( errtype );
202- if ( mesg == NULL && errnum == 0 ) free ( errdescr );
185+ char *res = strdup (buffer. c_str () );
186+ if ( res== NULL ) abort ( );
203187
204- return buffer ;
188+ return res ;
205189}
206190
207191/* Function to generate and write error logmessage (using vlogmsg) */
0 commit comments