Back to index

lightning-sunbird  0.9+nobinonly
ipcLog.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla IPC.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "ipcLog.h"
00039 
00040 #ifdef IPC_LOGGING
00041 
00042 #include <string.h>
00043 #include <ctype.h>
00044 
00045 #include "prenv.h"
00046 #include "prprf.h"
00047 #include "prthread.h"
00048 #include "plstr.h"
00049 
00050 PRBool ipcLogEnabled = PR_FALSE;
00051 char ipcLogPrefix[10] = {0};
00052 
00053 //-----------------------------------------------------------------------------
00054 // UNIX
00055 //-----------------------------------------------------------------------------
00056 #if defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
00057 #include <sys/types.h>
00058 #include <unistd.h>
00059 
00060 static inline PRUint32
00061 WritePrefix(char *buf, PRUint32 bufLen)
00062 {
00063     return PR_snprintf(buf, bufLen, "[%u:%p] %s ",
00064                        (unsigned) getpid(),
00065                        PR_GetCurrentThread(),
00066                        ipcLogPrefix);
00067 }
00068 #endif
00069 
00070 //-----------------------------------------------------------------------------
00071 // WIN32
00072 //-----------------------------------------------------------------------------
00073 #ifdef XP_WIN
00074 #include <windows.h>
00075 
00076 static inline PRUint32
00077 WritePrefix(char *buf, PRUint32 bufLen)
00078 {
00079     return PR_snprintf(buf, bufLen, "[%u:%p] %s ",
00080                        GetCurrentProcessId(),
00081                        PR_GetCurrentThread(),
00082                        ipcLogPrefix);
00083 }
00084 #endif
00085 
00086 //-----------------------------------------------------------------------------
00087 // logging API impl
00088 //-----------------------------------------------------------------------------
00089 
00090 void
00091 IPC_InitLog(const char *prefix)
00092 {
00093     if (PR_GetEnv("IPC_LOG_ENABLE")) {
00094         ipcLogEnabled = PR_TRUE;
00095         PL_strncpyz(ipcLogPrefix, prefix, sizeof(ipcLogPrefix));
00096     }
00097 }
00098 
00099 void
00100 IPC_Log(const char *fmt, ... )
00101 {
00102     va_list ap;
00103     va_start(ap, fmt);
00104     PRUint32 nb = 0;
00105     char buf[512];
00106 
00107     if (ipcLogPrefix[0])
00108         nb = WritePrefix(buf, sizeof(buf));
00109 
00110     PR_vsnprintf(buf + nb, sizeof(buf) - nb, fmt, ap);
00111     buf[sizeof(buf) - 1] = '\0';
00112 
00113     fwrite(buf, strlen(buf), 1, stdout);
00114 
00115     va_end(ap);
00116 }
00117 
00118 void
00119 IPC_LogBinary(const PRUint8 *data, PRUint32 len)
00120 {
00121     PRUint32 i, j, ln;
00122     for (i=0; i<len; ) {
00123         char line[100] = "";
00124         const PRUint8 *p;
00125 
00126         ln = 0;
00127         
00128         p = &data[i];
00129         for (j=0; j<PR_MIN(8, len - i); ++j, ++p)
00130             ln += PR_snprintf(line + ln, sizeof(line) - ln, "%02x  ", *p);
00131 
00132         for (; ln < 32; ++ln)
00133             line[ln] = ' ';
00134 
00135         p = &data[i];
00136         for (j=0; j<PR_MIN(8, len - i); ++j, ++p)
00137             ln += PR_snprintf(line + ln, sizeof(line) - ln, "%c", isprint(*p) ? *p : '.');
00138 
00139         line[ln] = '\0';
00140 
00141         i += (p - &data[i]);
00142 
00143         LOG(("%s\n", line));
00144     }
00145 }
00146 
00147 #endif