summaryrefslogtreecommitdiffstats
path: root/src/server/memlog.c
diff options
context:
space:
mode:
authorSimon Rettberg2013-08-01 18:14:15 +0200
committerSimon Rettberg2013-08-01 18:14:15 +0200
commit96b3a0feb963466447ca8dbc571bc5f670d533cc (patch)
treee546179f4d6d5cbb23dae202a8bf59b9f2ce95bb /src/server/memlog.c
parent[SERVER] Add inih (http://code.google.com/p/inih/) for *.ini parsing (diff)
downloaddnbd3-96b3a0feb963466447ca8dbc571bc5f670d533cc.tar.gz
dnbd3-96b3a0feb963466447ca8dbc571bc5f670d533cc.tar.xz
dnbd3-96b3a0feb963466447ca8dbc571bc5f670d533cc.zip
[SERVER] Add command line options to create empty image of certain size with empty cache map (so it needs an uplink server)
Diffstat (limited to 'src/server/memlog.c')
-rw-r--r--src/server/memlog.c75
1 files changed, 39 insertions, 36 deletions
diff --git a/src/server/memlog.c b/src/server/memlog.c
index f159d96..447d232 100644
--- a/src/server/memlog.c
+++ b/src/server/memlog.c
@@ -48,72 +48,75 @@ static volatile int bufferPos = 0;
void initmemlog()
{
// Use main spinlock to make sure we really init only once
- if (logBuffer) return;
- spin_init(&logLock, PTHREAD_PROCESS_PRIVATE);
- logBuffer = (LogLine *)calloc(LINE_COUNT, sizeof(LogLine));
+ if ( logBuffer ) return;
+ spin_init( &logLock, PTHREAD_PROCESS_PRIVATE );
+ logBuffer = (LogLine *)calloc( LINE_COUNT, sizeof(LogLine) );
}
void memlogf(const char *fmt, ...)
{
- if (!logBuffer) return; // Not initialized yet
+ if ( logBuffer == NULL ) {
+ va_list ap;
+ va_start( ap, fmt );
+ vprintf( fmt, ap );
+ va_end( ap );
+ return; // Not initialized yet
+ }
va_list ap;
int ret;
time_t rawtime;
struct tm *timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- spin_lock(&logLock);
- LogLine *const line = (LogLine *)&(logBuffer[bufferPos % LINE_COUNT]);
- const size_t offset = strftime(line->text, LINE_LEN, "[%d.%m. %H:%M:%S] ", timeinfo);
- if (offset == 0) *line->text = '\0';
- va_start(ap, fmt);
- ret = vsnprintf(line->text + offset, LINE_LEN - offset, fmt, ap);
- va_end(ap);
- char *end = line->text + strlen(line->text);
- while (end > line->text && *--end == '\n') *end = '\0'; // remove trailing \n
+ time( &rawtime );
+ timeinfo = localtime( &rawtime );
+ spin_lock( &logLock );
+ LogLine * const line = (LogLine *)&(logBuffer[bufferPos % LINE_COUNT]);
+ const size_t offset = strftime( line->text, LINE_LEN, "[%d.%m. %H:%M:%S] ", timeinfo );
+ if ( offset == 0 ) *line->text = '\0';
+ va_start( ap, fmt );
+ ret = vsnprintf( line->text + offset, LINE_LEN - offset, fmt, ap );
+ va_end( ap );
+ char *end = line->text + strlen( line->text );
+ while ( end > line->text && *--end == '\n' )
+ *end = '\0'; // remove trailing \n
// glibc 2.0 would return -1 if the buffer was too small
// glibc 2.1 would return the number of bytes required if the buffer was too small
// so to be safe either way, let strlen do the job
- line->len = strlen(line->text);
- if (ret > 0 || line->len > 0) ++bufferPos;
- spin_unlock(&logLock);
- puts(line->text);
+ line->len = strlen( line->text );
+ if ( ret > 0 || line->len > 0 ) ++bufferPos;
+ spin_unlock( &logLock );
+ puts( line->text );
}
char *fetchlog(int maxlines)
{
- if (!logBuffer) return NULL;
- if (maxlines <= 0 || maxlines > LINE_COUNT) maxlines = LINE_COUNT;
+ if ( !logBuffer ) return NULL ;
+ if ( maxlines <= 0 || maxlines > LINE_COUNT ) maxlines = LINE_COUNT;
const int start = MAX(0, bufferPos - maxlines);
int len = 1, i;
//printf("Outputting log from %d to %d\n", start, bufferPos);
- spin_lock(&logLock);
+ spin_lock( &logLock );
// Determine required buffer space for all log lines
- for (i = start; i < bufferPos; ++i)
- {
- if (logBuffer[i % LINE_COUNT].len > 0)
- {
+ for (i = start; i < bufferPos; ++i) {
+ if ( logBuffer[i % LINE_COUNT].len > 0 ) {
len += logBuffer[i % LINE_COUNT].len + 1;
}
}
//printf("Have to allocate %d bytes\n", len);
// Allocate buffer. If this is a bottleneck because of malloc, consider passing a buffer to the function that the caller allocates on the stack
- char *retval = (char *)calloc(len, sizeof(char));
- if (retval == NULL) goto endFunction;
+ char *retval = (char *)calloc( len, sizeof(char) );
+ if ( retval == NULL ) goto endFunction;
// Concatenate all log lines, delimit using '\n'
char *pos = retval;
- for (i = start; i < bufferPos; ++i)
- {
- LogLine *const line = (LogLine *)&(logBuffer[i % LINE_COUNT]);
- if (line->len > 0)
- {
- memcpy(pos, (char *)line->text, line->len);
+ for (i = start; i < bufferPos; ++i) {
+ LogLine * const line = (LogLine *)&(logBuffer[i % LINE_COUNT]);
+ if ( line->len > 0 ) {
+ memcpy( pos, (char *)line->text, line->len );
pos += line->len;
*pos++ = '\n';
}
}
*pos = '\0';
-endFunction:
- spin_unlock(&logLock);
+ endFunction:
+ spin_unlock( &logLock );
return retval;
}