michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "primpl.h" michael@0: #include "prenv.h" michael@0: #include "prprf.h" michael@0: #include michael@0: #ifdef ANDROID michael@0: #include michael@0: #endif michael@0: michael@0: /* michael@0: * Lock used to lock the log. michael@0: * michael@0: * We can't define _PR_LOCK_LOG simply as PR_Lock because PR_Lock may michael@0: * contain assertions. We have to avoid assertions in _PR_LOCK_LOG michael@0: * because PR_ASSERT calls PR_LogPrint, which in turn calls _PR_LOCK_LOG. michael@0: * This can lead to infinite recursion. michael@0: */ michael@0: static PRLock *_pr_logLock; michael@0: #if defined(_PR_PTHREADS) || defined(_PR_BTHREADS) michael@0: #define _PR_LOCK_LOG() PR_Lock(_pr_logLock); michael@0: #define _PR_UNLOCK_LOG() PR_Unlock(_pr_logLock); michael@0: #elif defined(_PR_GLOBAL_THREADS_ONLY) michael@0: #define _PR_LOCK_LOG() { _PR_LOCK_LOCK(_pr_logLock) michael@0: #define _PR_UNLOCK_LOG() _PR_LOCK_UNLOCK(_pr_logLock); } michael@0: #else michael@0: michael@0: #define _PR_LOCK_LOG() \ michael@0: { \ michael@0: PRIntn _is; \ michael@0: PRThread *_me = _PR_MD_CURRENT_THREAD(); \ michael@0: if (!_PR_IS_NATIVE_THREAD(_me)) \ michael@0: _PR_INTSOFF(_is); \ michael@0: _PR_LOCK_LOCK(_pr_logLock) michael@0: michael@0: #define _PR_UNLOCK_LOG() \ michael@0: _PR_LOCK_UNLOCK(_pr_logLock); \ michael@0: PR_ASSERT(_me == _PR_MD_CURRENT_THREAD()); \ michael@0: if (!_PR_IS_NATIVE_THREAD(_me)) \ michael@0: _PR_INTSON(_is); \ michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #if defined(XP_PC) michael@0: #define strcasecmp stricmp michael@0: #endif michael@0: michael@0: /* michael@0: * On NT, we can't define _PUT_LOG as PR_Write or _PR_MD_WRITE, michael@0: * because every asynchronous file io operation leads to a fiber context michael@0: * switch. So we define _PUT_LOG as fputs (from stdio.h). A side michael@0: * benefit is that fputs handles the LF->CRLF translation. This michael@0: * code can also be used on other platforms with file stream io. michael@0: */ michael@0: #if defined(WIN32) || defined(XP_OS2) michael@0: #define _PR_USE_STDIO_FOR_LOGGING michael@0: #endif michael@0: michael@0: /* michael@0: ** Coerce Win32 log output to use OutputDebugString() when michael@0: ** NSPR_LOG_FILE is set to "WinDebug". michael@0: */ michael@0: #if defined(XP_PC) michael@0: #define WIN32_DEBUG_FILE (FILE*)-2 michael@0: #endif michael@0: michael@0: #ifdef WINCE michael@0: static void OutputDebugStringA(const char* msg) { michael@0: int len = MultiByteToWideChar(CP_ACP, 0, msg, -1, 0, 0); michael@0: WCHAR *wMsg = (WCHAR *)PR_Malloc(len * sizeof(WCHAR)); michael@0: MultiByteToWideChar(CP_ACP, 0, msg, -1, wMsg, len); michael@0: OutputDebugStringW(wMsg); michael@0: PR_Free(wMsg); michael@0: } michael@0: #endif michael@0: michael@0: /* Macros used to reduce #ifdef pollution */ michael@0: michael@0: #if defined(_PR_USE_STDIO_FOR_LOGGING) && defined(XP_PC) michael@0: #define _PUT_LOG(fd, buf, nb) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (logFile == WIN32_DEBUG_FILE) { \ michael@0: char savebyte = buf[nb]; \ michael@0: buf[nb] = '\0'; \ michael@0: OutputDebugStringA(buf); \ michael@0: buf[nb] = savebyte; \ michael@0: } else { \ michael@0: fwrite(buf, 1, nb, fd); \ michael@0: fflush(fd); \ michael@0: } \ michael@0: PR_END_MACRO michael@0: #elif defined(_PR_USE_STDIO_FOR_LOGGING) michael@0: #define _PUT_LOG(fd, buf, nb) {fwrite(buf, 1, nb, fd); fflush(fd);} michael@0: #elif defined(ANDROID) michael@0: #define _PUT_LOG(fd, buf, nb) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (fd == _pr_stderr) { \ michael@0: char savebyte = buf[nb]; \ michael@0: buf[nb] = '\0'; \ michael@0: __android_log_write(ANDROID_LOG_INFO, "PRLog", buf); \ michael@0: buf[nb] = savebyte; \ michael@0: } else { \ michael@0: PR_Write(fd, buf, nb); \ michael@0: } \ michael@0: PR_END_MACRO michael@0: #elif defined(_PR_PTHREADS) michael@0: #define _PUT_LOG(fd, buf, nb) PR_Write(fd, buf, nb) michael@0: #else michael@0: #define _PUT_LOG(fd, buf, nb) _PR_MD_WRITE(fd, buf, nb) michael@0: #endif michael@0: michael@0: /************************************************************************/ michael@0: michael@0: static PRLogModuleInfo *logModules; michael@0: michael@0: static char *logBuf = NULL; michael@0: static char *logp; michael@0: static char *logEndp; michael@0: #ifdef _PR_USE_STDIO_FOR_LOGGING michael@0: static FILE *logFile = NULL; michael@0: #else michael@0: static PRFileDesc *logFile = 0; michael@0: #endif michael@0: static PRBool outputTimeStamp = PR_FALSE; michael@0: static PRBool appendToLog = PR_FALSE; michael@0: michael@0: #define LINE_BUF_SIZE 512 michael@0: #define DEFAULT_BUF_SIZE 16384 michael@0: michael@0: #ifdef _PR_NEED_STRCASECMP michael@0: michael@0: /* michael@0: * strcasecmp is defined in /usr/ucblib/libucb.a on some platforms michael@0: * such as NCR and Unixware. Linking with both libc and libucb michael@0: * may cause some problem, so I just provide our own implementation michael@0: * of strcasecmp here. michael@0: */ michael@0: michael@0: static const unsigned char uc[] = michael@0: { michael@0: '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007', michael@0: '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017', michael@0: '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027', michael@0: '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037', michael@0: ' ', '!', '"', '#', '$', '%', '&', '\'', michael@0: '(', ')', '*', '+', ',', '-', '.', '/', michael@0: '0', '1', '2', '3', '4', '5', '6', '7', michael@0: '8', '9', ':', ';', '<', '=', '>', '?', michael@0: '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', michael@0: 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', michael@0: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', michael@0: 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', michael@0: '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', michael@0: 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', michael@0: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', michael@0: 'X', 'Y', 'Z', '{', '|', '}', '~', '\177' michael@0: }; michael@0: michael@0: PRIntn strcasecmp(const char *a, const char *b) michael@0: { michael@0: const unsigned char *ua = (const unsigned char *)a; michael@0: const unsigned char *ub = (const unsigned char *)b; michael@0: michael@0: if( ((const char *)0 == a) || (const char *)0 == b ) michael@0: return (PRIntn)(a-b); michael@0: michael@0: while( (uc[*ua] == uc[*ub]) && ('\0' != *a) ) michael@0: { michael@0: a++; michael@0: ua++; michael@0: ub++; michael@0: } michael@0: michael@0: return (PRIntn)(uc[*ua] - uc[*ub]); michael@0: } michael@0: michael@0: #endif /* _PR_NEED_STRCASECMP */ michael@0: michael@0: void _PR_InitLog(void) michael@0: { michael@0: char *ev; michael@0: michael@0: _pr_logLock = PR_NewLock(); michael@0: michael@0: ev = PR_GetEnv("NSPR_LOG_MODULES"); michael@0: if (ev && ev[0]) { michael@0: char module[64]; /* Security-Critical: If you change this michael@0: * size, you must also change the sscanf michael@0: * format string to be size-1. michael@0: */ michael@0: PRBool isSync = PR_FALSE; michael@0: PRIntn evlen = strlen(ev), pos = 0; michael@0: PRInt32 bufSize = DEFAULT_BUF_SIZE; michael@0: while (pos < evlen) { michael@0: PRIntn level = 1, count = 0, delta = 0; michael@0: count = sscanf(&ev[pos], "%63[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-]%n:%d%n", michael@0: module, &delta, &level, &delta); michael@0: pos += delta; michael@0: if (count == 0) break; michael@0: michael@0: /* michael@0: ** If count == 2, then we got module and level. If count michael@0: ** == 1, then level defaults to 1 (module enabled). michael@0: */ michael@0: if (strcasecmp(module, "sync") == 0) { michael@0: isSync = PR_TRUE; michael@0: } else if (strcasecmp(module, "bufsize") == 0) { michael@0: if (level >= LINE_BUF_SIZE) { michael@0: bufSize = level; michael@0: } michael@0: } else if (strcasecmp(module, "timestamp") == 0) { michael@0: outputTimeStamp = PR_TRUE; michael@0: } else if (strcasecmp(module, "append") == 0) { michael@0: appendToLog = PR_TRUE; michael@0: } else { michael@0: PRLogModuleInfo *lm = logModules; michael@0: PRBool skip_modcheck = michael@0: (0 == strcasecmp (module, "all")) ? PR_TRUE : PR_FALSE; michael@0: michael@0: while (lm != NULL) { michael@0: if (skip_modcheck) lm -> level = (PRLogModuleLevel)level; michael@0: else if (strcasecmp(module, lm->name) == 0) { michael@0: lm->level = (PRLogModuleLevel)level; michael@0: break; michael@0: } michael@0: lm = lm->next; michael@0: } michael@0: } michael@0: /*found:*/ michael@0: count = sscanf(&ev[pos], " , %n", &delta); michael@0: pos += delta; michael@0: if (count == EOF) break; michael@0: } michael@0: PR_SetLogBuffering(isSync ? 0 : bufSize); michael@0: michael@0: #ifdef XP_UNIX michael@0: if ((getuid() != geteuid()) || (getgid() != getegid())) { michael@0: return; michael@0: } michael@0: #endif /* XP_UNIX */ michael@0: michael@0: ev = PR_GetEnv("NSPR_LOG_FILE"); michael@0: if (ev && ev[0]) { michael@0: if (!PR_SetLogFile(ev)) { michael@0: #ifdef XP_PC michael@0: char* str = PR_smprintf("Unable to create nspr log file '%s'\n", ev); michael@0: if (str) { michael@0: OutputDebugStringA(str); michael@0: PR_smprintf_free(str); michael@0: } michael@0: #else michael@0: fprintf(stderr, "Unable to create nspr log file '%s'\n", ev); michael@0: #endif michael@0: } michael@0: } else { michael@0: #ifdef _PR_USE_STDIO_FOR_LOGGING michael@0: logFile = stderr; michael@0: #else michael@0: logFile = _pr_stderr; michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: michael@0: void _PR_LogCleanup(void) michael@0: { michael@0: PRLogModuleInfo *lm = logModules; michael@0: michael@0: PR_LogFlush(); michael@0: michael@0: #ifdef _PR_USE_STDIO_FOR_LOGGING michael@0: if (logFile michael@0: && logFile != stdout michael@0: && logFile != stderr michael@0: #ifdef XP_PC michael@0: && logFile != WIN32_DEBUG_FILE michael@0: #endif michael@0: ) { michael@0: fclose(logFile); michael@0: } michael@0: #else michael@0: if (logFile && logFile != _pr_stdout && logFile != _pr_stderr) { michael@0: PR_Close(logFile); michael@0: } michael@0: #endif michael@0: logFile = NULL; michael@0: michael@0: if (logBuf) michael@0: PR_DELETE(logBuf); michael@0: michael@0: while (lm != NULL) { michael@0: PRLogModuleInfo *next = lm->next; michael@0: free((/*const*/ char *)lm->name); michael@0: PR_Free(lm); michael@0: lm = next; michael@0: } michael@0: logModules = NULL; michael@0: michael@0: if (_pr_logLock) { michael@0: PR_DestroyLock(_pr_logLock); michael@0: _pr_logLock = NULL; michael@0: } michael@0: } michael@0: michael@0: static void _PR_SetLogModuleLevel( PRLogModuleInfo *lm ) michael@0: { michael@0: char *ev; michael@0: michael@0: ev = PR_GetEnv("NSPR_LOG_MODULES"); michael@0: if (ev && ev[0]) { michael@0: char module[64]; /* Security-Critical: If you change this michael@0: * size, you must also change the sscanf michael@0: * format string to be size-1. michael@0: */ michael@0: PRIntn evlen = strlen(ev), pos = 0; michael@0: while (pos < evlen) { michael@0: PRIntn level = 1, count = 0, delta = 0; michael@0: michael@0: count = sscanf(&ev[pos], "%63[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-]%n:%d%n", michael@0: module, &delta, &level, &delta); michael@0: pos += delta; michael@0: if (count == 0) break; michael@0: michael@0: /* michael@0: ** If count == 2, then we got module and level. If count michael@0: ** == 1, then level defaults to 1 (module enabled). michael@0: */ michael@0: if (lm != NULL) michael@0: { michael@0: if ((strcasecmp(module, "all") == 0) michael@0: || (strcasecmp(module, lm->name) == 0)) michael@0: { michael@0: lm->level = (PRLogModuleLevel)level; michael@0: } michael@0: } michael@0: count = sscanf(&ev[pos], " , %n", &delta); michael@0: pos += delta; michael@0: if (count == EOF) break; michael@0: } michael@0: } michael@0: } /* end _PR_SetLogModuleLevel() */ michael@0: michael@0: PR_IMPLEMENT(PRLogModuleInfo*) PR_NewLogModule(const char *name) michael@0: { michael@0: PRLogModuleInfo *lm; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: lm = PR_NEWZAP(PRLogModuleInfo); michael@0: if (lm) { michael@0: lm->name = strdup(name); michael@0: lm->level = PR_LOG_NONE; michael@0: lm->next = logModules; michael@0: logModules = lm; michael@0: _PR_SetLogModuleLevel(lm); michael@0: } michael@0: return lm; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRBool) PR_SetLogFile(const char *file) michael@0: { michael@0: #ifdef _PR_USE_STDIO_FOR_LOGGING michael@0: FILE *newLogFile; michael@0: michael@0: #ifdef XP_PC michael@0: if ( strcmp( file, "WinDebug") == 0) michael@0: { michael@0: newLogFile = WIN32_DEBUG_FILE; michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: const char *mode = appendToLog ? "a" : "w"; michael@0: newLogFile = fopen(file, mode); michael@0: if (!newLogFile) michael@0: return PR_FALSE; michael@0: michael@0: #ifndef WINCE /* _IONBF does not exist in the Windows Mobile 6 SDK. */ michael@0: /* We do buffering ourselves. */ michael@0: setvbuf(newLogFile, NULL, _IONBF, 0); michael@0: #endif michael@0: } michael@0: if (logFile michael@0: && logFile != stdout michael@0: && logFile != stderr michael@0: #ifdef XP_PC michael@0: && logFile != WIN32_DEBUG_FILE michael@0: #endif michael@0: ) { michael@0: fclose(logFile); michael@0: } michael@0: logFile = newLogFile; michael@0: return PR_TRUE; michael@0: #else michael@0: PRFileDesc *newLogFile; michael@0: PRIntn flags = PR_WRONLY|PR_CREATE_FILE; michael@0: if (appendToLog) { michael@0: flags |= PR_APPEND; michael@0: } else { michael@0: flags |= PR_TRUNCATE; michael@0: } michael@0: michael@0: newLogFile = PR_Open(file, flags, 0666); michael@0: if (newLogFile) { michael@0: if (logFile && logFile != _pr_stdout && logFile != _pr_stderr) { michael@0: PR_Close(logFile); michael@0: } michael@0: logFile = newLogFile; michael@0: } michael@0: return (PRBool) (newLogFile != 0); michael@0: #endif /* _PR_USE_STDIO_FOR_LOGGING */ michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_SetLogBuffering(PRIntn buffer_size) michael@0: { michael@0: PR_LogFlush(); michael@0: michael@0: if (logBuf) michael@0: PR_DELETE(logBuf); michael@0: michael@0: if (buffer_size >= LINE_BUF_SIZE) { michael@0: logp = logBuf = (char*) PR_MALLOC(buffer_size); michael@0: logEndp = logp + buffer_size; michael@0: } michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_LogPrint(const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: char line[LINE_BUF_SIZE]; michael@0: char *line_long = NULL; michael@0: PRUint32 nb_tid = 0, nb; michael@0: PRThread *me; michael@0: PRExplodedTime now; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: if (!logFile) { michael@0: return; michael@0: } michael@0: michael@0: if (outputTimeStamp) { michael@0: PR_ExplodeTime(PR_Now(), PR_GMTParameters, &now); michael@0: nb_tid = PR_snprintf(line, sizeof(line)-1, michael@0: "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - ", michael@0: now.tm_year, now.tm_month + 1, now.tm_mday, michael@0: now.tm_hour, now.tm_min, now.tm_sec, michael@0: now.tm_usec); michael@0: } michael@0: michael@0: me = PR_GetCurrentThread(); michael@0: nb_tid += PR_snprintf(line+nb_tid, sizeof(line)-nb_tid-1, "%ld[%p]: ", michael@0: #if defined(_PR_BTHREADS) michael@0: me, me); michael@0: #else michael@0: me ? me->id : 0L, me); michael@0: #endif michael@0: michael@0: va_start(ap, fmt); michael@0: nb = nb_tid + PR_vsnprintf(line+nb_tid, sizeof(line)-nb_tid-1, fmt, ap); michael@0: va_end(ap); michael@0: michael@0: /* michael@0: * Check if we might have run out of buffer space (in case we have a michael@0: * long line), and malloc a buffer just this once. michael@0: */ michael@0: if (nb == sizeof(line)-2) { michael@0: va_start(ap, fmt); michael@0: line_long = PR_vsmprintf(fmt, ap); michael@0: va_end(ap); michael@0: /* If this failed, we'll fall back to writing the truncated line. */ michael@0: } michael@0: michael@0: if (line_long) { michael@0: nb = strlen(line_long); michael@0: _PR_LOCK_LOG(); michael@0: if (logBuf != 0) { michael@0: _PUT_LOG(logFile, logBuf, logp - logBuf); michael@0: logp = logBuf; michael@0: } michael@0: /* michael@0: * Write out the thread id (with an optional timestamp) and the michael@0: * malloc'ed buffer. michael@0: */ michael@0: _PUT_LOG(logFile, line, nb_tid); michael@0: _PUT_LOG(logFile, line_long, nb); michael@0: /* Ensure there is a trailing newline. */ michael@0: if (!nb || (line_long[nb-1] != '\n')) { michael@0: char eol[2]; michael@0: eol[0] = '\n'; michael@0: eol[1] = '\0'; michael@0: _PUT_LOG(logFile, eol, 1); michael@0: } michael@0: _PR_UNLOCK_LOG(); michael@0: PR_smprintf_free(line_long); michael@0: } else { michael@0: /* Ensure there is a trailing newline. */ michael@0: if (nb && (line[nb-1] != '\n')) { michael@0: line[nb++] = '\n'; michael@0: line[nb] = '\0'; michael@0: } michael@0: _PR_LOCK_LOG(); michael@0: if (logBuf == 0) { michael@0: _PUT_LOG(logFile, line, nb); michael@0: } else { michael@0: /* If nb can't fit into logBuf, write out logBuf first. */ michael@0: if (logp + nb > logEndp) { michael@0: _PUT_LOG(logFile, logBuf, logp - logBuf); michael@0: logp = logBuf; michael@0: } michael@0: /* nb is guaranteed to fit into logBuf. */ michael@0: memcpy(logp, line, nb); michael@0: logp += nb; michael@0: } michael@0: _PR_UNLOCK_LOG(); michael@0: } michael@0: PR_LogFlush(); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_LogFlush(void) michael@0: { michael@0: if (logBuf && logFile) { michael@0: _PR_LOCK_LOG(); michael@0: if (logp > logBuf) { michael@0: _PUT_LOG(logFile, logBuf, logp - logBuf); michael@0: logp = logBuf; michael@0: } michael@0: _PR_UNLOCK_LOG(); michael@0: } michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_Abort(void) michael@0: { michael@0: PR_LogPrint("Aborting"); michael@0: abort(); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_Assert(const char *s, const char *file, PRIntn ln) michael@0: { michael@0: PR_LogPrint("Assertion failure: %s, at %s:%d\n", s, file, ln); michael@0: fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln); michael@0: fflush(stderr); michael@0: #ifdef WIN32 michael@0: DebugBreak(); michael@0: #endif michael@0: #ifdef XP_OS2 michael@0: asm("int $3"); michael@0: #endif michael@0: abort(); michael@0: }