1 /*
2 * Copyright 2007 The Android Open Source Project
3 *
4 * Debug-logging code.
5 */
6 #include "Common.h"
7
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <time.h>
11
12 /*
13 * Write a message to our private log file. This is a little awkward since
14 * some or all of the system calls we want to use are being intercepted.
15 */
wsLog(const char * format,...)16 void wsLog(const char* format, ...)
17 {
18 #if defined(HAVE_LOCALTIME_R)
19 struct tm tmBuf;
20 #endif
21 struct tm* ptm;
22 time_t now;
23 char timeBuf[32];
24 char prefixBuf[64];
25 int prefixLen;
26 char msgBuf[256];
27 int msgLen;
28
29 if (gWrapSim.logFd < 0)
30 return;
31
32 /*
33 * Create a prefix with a timestamp.
34 */
35 now = time(NULL);
36 #if defined(HAVE_LOCALTIME_R)
37 ptm = localtime_r(&now, &tmBuf);
38 #else
39 ptm = localtime(&now);
40 #endif
41 //strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
42 strftime(timeBuf, sizeof(timeBuf), "%H:%M:%S", ptm);
43
44 prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%s %5d ",
45 timeBuf, (int) getpid());
46
47 /*
48 * Format the message into a buffer.
49 */
50 va_list args;
51
52 va_start(args, format);
53 msgLen = vsnprintf(msgBuf, sizeof(msgBuf), format, args);
54 va_end(args);
55
56 /* if we overflowed, trim and annotate */
57 if (msgLen >= (int) sizeof(msgBuf)) {
58 msgBuf[sizeof(msgBuf)-2] = '!';
59 msgBuf[sizeof(msgBuf)-1] = '\n';
60 msgLen = sizeof(msgBuf);
61 }
62
63 /*
64 * Write the whole thing in one shot. The log file was opened with
65 * O_APPEND so we don't have to worry about clashes.
66 */
67 struct iovec logVec[2];
68 logVec[0].iov_base = prefixBuf;
69 logVec[0].iov_len = prefixLen;
70 logVec[1].iov_base = msgBuf;
71 logVec[1].iov_len = msgLen;
72 (void) _ws_writev(gWrapSim.logFd, logVec, 2);
73 }
74
75