# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

net_displayer.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdnet.h"
00027 
00028 #include "nel/net/net_displayer.h"
00029 #include "nel/net/message.h"
00030 #include "nel/net/naming_client.h"
00031 
00032 
00033 using namespace std;
00034 using namespace NLMISC;
00035 
00036 namespace NLNET {
00037 
00038 
00039 /* This index must correspond to the index for "LOG" in CallbackArray in the Logging Service
00040  * (see CNetDisplayer::display())
00041  */
00042 const sint16 LOG_CBINDEX = 0;
00043 
00044 
00045 /*
00046  * Constructor
00047  */
00048 CNetDisplayer::CNetDisplayer(bool autoConnect) :
00049         _Server(NULL), _ServerAllocated (false) // disable logging otherwise an infinite recursion may occur
00050 {
00051         if (autoConnect) findAndConnect();
00052 }
00053 
00054 
00055 /*
00056  * Find the server (using the NS) and connect
00057  */
00058 void CNetDisplayer::findAndConnect()
00059 {
00060         if (_Server == NULL)
00061         {
00062                 _Server = new CCallbackClient();
00063                 _ServerAllocated = true;
00064         }
00065 
00066         if ( CNamingClient::lookupAndConnect( "LOGS", *_Server ) )
00067         {
00068                 nldebug( "Connected to logging service" );
00069         }
00070 }
00071 
00072 /*
00073  * Sets logging server address
00074  */
00075 void CNetDisplayer::setLogServer (const CInetAddress& logServerAddr)
00076 {
00077         if (_Server != NULL && _Server->connected()) return;
00078 
00079         _ServerAddr = logServerAddr;
00080 
00081         if (_Server == NULL)
00082         {
00083                 _Server = new CCallbackClient();
00084                 _ServerAllocated = true;
00085         }
00086         
00087         try
00088         {
00089                 _Server->connect (_ServerAddr);
00090         }
00091         catch( ESocket& )
00092         {
00093                 // Silence
00094         }
00095 }
00096 
00097 void CNetDisplayer::setLogServer (CCallbackClient *server)
00098 {
00099         if (_Server != NULL && _Server->connected()) return;
00100 
00101         _Server = server;
00102 }
00103 
00104 
00105 /*
00106  * Destructor
00107  */
00108 CNetDisplayer::~CNetDisplayer ()
00109 {
00110         if (_ServerAllocated)
00111         {
00112                 _Server->disconnect ();
00113                 delete _Server;
00114         }
00115 }
00116 
00117 
00118 /*
00119  * Sends the string to the logging server
00120  *
00121  * Log format: "2000/01/15 12:05:30 <LogType> <ProcessName>: <Msg>"
00122  */
00123 void CNetDisplayer::doDisplay ( const TDisplayInfo& args, const char *message)
00124 {
00125         try
00126         {
00127                 if (_Server == NULL || !_Server->connected())
00128                 {
00129                         return;
00130                 }
00131 
00132                 bool needSpace = false;
00133                 stringstream ss;
00134 
00135                 if (args.Date != 0)
00136                 {
00137                         ss << dateToHumanString(args.Date);
00138                         needSpace = true;
00139                 }
00140 
00141                 if (args.LogType != CLog::LOG_NO)
00142                 {
00143                         if (needSpace) { ss << " "; needSpace = false; }
00144                         ss << logTypeToString(args.LogType);
00145                         needSpace = true;
00146                 }
00147 
00148                 if (!args.ProcessName.empty())
00149                 {
00150                         if (needSpace) { ss << " "; needSpace = false; }
00151                         ss << args.ProcessName;
00152                         needSpace = true;
00153                 }
00154                 
00155                 if (needSpace) { ss << ": "; needSpace = false; }
00156 
00157                 ss << message;
00158 
00159                 CMessage msg(_Server->getSIDA(), "LOG" );
00160                 string s = ss.str();
00161                 msg.serial( s );
00162                 _Server->send (msg, 0, false);
00163         }
00164         catch( NLMISC::Exception& )
00165         {
00166                 // Silence
00167         }
00168 }
00169 
00170 
00171 } // NLNET