Back to index

lightning-sunbird  0.9+nobinonly
nclog.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 NCLog.
00015  *
00016  * The Initial Developer of the Original Code is Marco Manfredini.
00017  *
00018  * Code posted to USENET:
00019  *
00020  *     http://groups-beta.google.com/group/microsoft.public.windowsce.embedded/
00021  *            browse_thread/thread/352904e5ff1bfeb/
00022  *            9ad05ef17dda0203?q=outputdebugstring+wince#9ad05ef17dda0203
00023  *
00024  * Portions created by the Initial Developer are Copyright (C) 2005
00025  * the Initial Developer. All Rights Reserved.
00026  *
00027  * Contributor(s):
00028  *    Doug Turner <dougt@meer.net>
00029  * 
00030  * Alternatively, the contents of this file may be used under the terms of
00031  * either the GNU General Public License Version 2 or later (the "GPL"), or
00032  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00033  * in which case the provisions of the GPL or the LGPL are applicable instead
00034  * of those above. If you wish to allow use of your version of this file only
00035  * under the terms of either the GPL or the LGPL, and not to allow others to
00036  * use your version of this file under the terms of the MPL, indicate your
00037  * decision by deleting the provisions above and replace them with the notice
00038  * and other provisions required by the GPL or the LGPL. If you do not delete
00039  * the provisions above, a recipient may use your version of this file under
00040  * the terms of any one of the MPL, the GPL or the LGPL.
00041  *
00042  * ***** END LICENSE BLOCK ***** */
00043 
00044 
00045 #include "mozce_internal.h"
00046 
00047 #define USE_NC_LOGGING
00048 
00049 #ifdef USE_NC_LOGGING
00050 
00051 
00052 #include "winsock.h"
00053 #include <stdarg.h>
00054 #include <stdio.h>
00055 
00056 static SOCKET wsa_socket=INVALID_SOCKET;
00057 #pragma comment(lib , "winsock")
00058 
00059 static unsigned short theLogPort;
00060 
00061 // bind the log socket to a specific port.
00062 static bool wsa_bind(unsigned short port)
00063 {
00064   SOCKADDR_IN addr;
00065   addr.sin_family = AF_INET;
00066   addr.sin_port = htons(port);
00067   addr.sin_addr.s_addr = htonl(INADDR_ANY);
00068   int r=bind(wsa_socket,(sockaddr*)&addr,sizeof(addr));
00069   if (r==0) theLogPort=port;
00070   return (r==0);
00071   
00072 }
00073 
00074 // initialize everything, if the socket isn't open.
00075 static bool wsa_init()
00076 {
00077   if (wsa_socket != INVALID_SOCKET) return true;
00078   int r;
00079   WSADATA wd;
00080   BOOL bc=true;
00081   
00082   if (0 != WSAStartup(0x101, &wd)) 
00083   {
00084          MessageBox(0, L"WSAStartup failed", L"ERROR", 0);
00085          goto error;
00086   }
00087   wsa_socket=socket(PF_INET, SOCK_DGRAM, 0);
00088   if (wsa_socket == INVALID_SOCKET)
00089   {
00090          MessageBox(0, L"socket failed", L"ERROR", 0);
00091          goto error;
00092   }
00093   r=setsockopt(wsa_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bc,
00094                sizeof(bc));
00095   if (r!=0)
00096   {
00097        MessageBox(0, L"setsockopt failed", L"ERROR", 0);
00098        goto error;
00099   }
00100 
00101   if (wsa_bind(9998)) return true; // bind to default port.
00102 
00103   MessageBox(0, L"Can Not Bind To Port", L"ERROR", 0);
00104 
00105 error:
00106   if (wsa_socket != INVALID_SOCKET) closesocket(wsa_socket);
00107   return false;
00108 
00109 }
00110 
00111 // can be called externally to select a different port for operations
00112 bool set_nclog_port(unsigned short x) { return wsa_bind(x); }
00113 
00114 static void wsa_send(const char *x)
00115 {
00116   SOCKADDR_IN sa;
00117   sa.sin_family = AF_INET;
00118   sa.sin_port = htons(theLogPort);
00119   sa.sin_addr.s_addr = htonl(INADDR_BROADCAST);
00120   
00121   sendto(wsa_socket, x, strlen(x), 0, (sockaddr*)&sa, sizeof(sa));
00122 }
00123 
00124 // format input, convert to 8-bit and send.
00125 int nclog (const char *fmt, ...)
00126 {
00127   va_list vl;
00128   va_start(vl,fmt);
00129   char buf[1024]; // to bad CE hasn't got wvnsprintf
00130   sprintf(buf,fmt,vl);
00131   wsa_init();
00132   wsa_send(buf);
00133   return 0;
00134 }
00135 
00136 void nclograw(const char* data, long length)
00137 {
00138   wsa_init();
00139   
00140   SOCKADDR_IN sa;
00141   sa.sin_family = AF_INET;
00142   sa.sin_port = htons(theLogPort);
00143   sa.sin_addr.s_addr = htonl(INADDR_BROADCAST);
00144   
00145   sendto(wsa_socket, data, length, 0, (sockaddr*)&sa, sizeof(sa));
00146 }
00147 
00148 // finalize the socket on program termination.
00149 struct _nclog_module
00150 {
00151   ~_nclog_module()
00152   {
00153     if (wsa_socket!=INVALID_SOCKET)
00154     {
00155       nclog("nclog goes down\n");
00156       shutdown(wsa_socket,2);
00157       closesocket(wsa_socket);
00158     }
00159   }
00160   
00161 };
00162 
00163 static _nclog_module module; 
00164 
00165 
00166 #endif