Log.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021   Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include "eyedb_p.h"
00026 
00027 namespace eyedb {
00028 
00029   Status Log::setLogMask(LogMask mask)
00030   {
00031     eyedblib::log_mask = mask;
00032     return Success;
00033   }
00034 
00035   Status Log::setLogMask(const char *str)
00036   {
00037     LogMask mask;
00038     Status s = logStringToMask(str, mask);
00039     if (s) return s;
00040     eyedblib::log_mask = mask;
00041     return Success;
00042   }
00043 
00044   LogMask Log::getLogMask()
00045   {
00046     return eyedblib::log_mask;
00047   }
00048 
00049   static const char *progName;
00050   static const char *logName;
00051 
00052   static Bool log_date;
00053   static Bool log_timer;
00054   static Bool log_pid;
00055   static Bool log_progname;
00056 
00057   static const char logdev_var[] = "logdev";
00058   static const char logmask_var[] = "logmask";
00059 
00060   Status Log::init(const char *_progName, const char *_logName)
00061   {
00062     progName = _progName;
00063     logName = (_logName ? _logName : eyedb::ServerConfig::getSValue(logdev_var));
00064 
00065     if (!logName)
00066       logName = eyedb::ClientConfig::getCValue(logdev_var);
00067 
00068     utlogInit(progName, logName);
00069 
00070     setLogDate(log_date);
00071     setLogTimer(log_timer);
00072     setLogPid(log_pid);
00073     setLogProgName(log_progname);
00074 
00075     if (getLogMask())
00076       return Success;
00077 
00078     const char *s = eyedb::ServerConfig::getSValue(logmask_var);
00079     if (!s)
00080       s = eyedb::ClientConfig::getCValue(logmask_var);
00081 
00082     if (s)
00083       return Log::setLogMask(s);
00084 
00085     return Log::setLogMask(IDB_LOG_DEFAULT);
00086   }
00087 
00088   const char *Log::getLog()
00089   {
00090     return logName;
00091   }
00092 
00093   void Log::setLog(const char *_logName)
00094   {
00095     logName = _logName;
00096     utlogInit(progName, logName);
00097   }
00098 
00099   void Log::setLogTimer(Bool on)
00100   {
00101     log_timer = on;
00102     utlogSetLogTimer(on);
00103   }
00104 
00105   Bool Log::getLogTimer()
00106   {
00107     return log_timer;
00108   }
00109 
00110   void Log::resetLogTimer()
00111   {
00112     utlogResetTimer();
00113   }
00114 
00115   void Log::setLogDate(Bool on)
00116   {
00117     log_date = on;
00118     utlogSetLogDate(on);
00119   }
00120 
00121   Bool Log::getLogDate()
00122   {
00123     return log_date;
00124   }
00125 
00126   void Log::setLogPid(Bool on)
00127   {
00128     log_pid = on;
00129     utlogSetLogPid(on);
00130   }
00131 
00132   Bool Log::getLogPid()
00133   {
00134     return log_pid;
00135   }
00136 
00137   void Log::setLogProgName(Bool on)
00138   {
00139     log_progname = on;
00140     utlogSetLogProgName(on);
00141   }
00142 
00143   Bool Log::getLogProgName()
00144   {
00145     return log_progname;
00146   }
00147 
00148 #define APPEND(S) do {if (!first) str += "+"; first = 0; str += (S);} while(0)
00149 
00150   Status Log::logMaskToString(LogMask mask, std::string &str)
00151   {
00152     str = "";
00153     int first = 1;
00154 
00155     if (mask & IDB_LOG_LOCAL) APPEND("local");
00156     if (mask & IDB_LOG_SERVER) APPEND("server");
00157     if (mask & IDB_LOG_CONN) APPEND("connection");
00158     if (mask & IDB_LOG_TRANSACTION) APPEND("transaction");
00159     if (mask & IDB_LOG_DATABASE) APPEND("database");
00160     if (mask & IDB_LOG_ADMIN) APPEND("admin");
00161     if (mask & IDB_LOG_EXCEPTION) APPEND("exception");
00162     if (mask & IDB_LOG_OID_CREATE) APPEND("oid:create");
00163     if (mask & IDB_LOG_OID_READ) APPEND("oid:read");
00164     if (mask & IDB_LOG_OID_WRITE) APPEND("oid:write");
00165     if (mask & IDB_LOG_OID_DELETE) APPEND("oid:delete");
00166     if (mask & IDB_LOG_MMAP) APPEND("memory:map");
00167     if (mask & IDB_LOG_MMAP_DETAIL) APPEND("memory:map:detail");
00168     if (mask & IDB_LOG_MTX) APPEND("mutex");
00169     if (mask & IDB_LOG_IDX_CREATE) APPEND("index:create");
00170     if (mask & IDB_LOG_IDX_REMOVE) APPEND("index:remove");
00171     if (mask & IDB_LOG_IDX_INSERT) APPEND("index:insert");
00172     if (mask & IDB_LOG_IDX_SUPPRESS) APPEND("index:suppress");
00173     if (mask & IDB_LOG_IDX_SEARCH) APPEND("index:search");
00174     if (mask & IDB_LOG_IDX_SEARCH_DETAIL) APPEND("index:search:detail");
00175     if (mask & IDB_LOG_DEV) APPEND("dev");
00176     if (mask & IDB_LOG_OBJ_LOAD) APPEND("object:load");
00177     if (mask & IDB_LOG_OBJ_CREATE) APPEND("object:create");
00178     if (mask & IDB_LOG_OBJ_UPDATE) APPEND("object:update");
00179     if (mask & IDB_LOG_OBJ_REMOVE) APPEND("object:remove");
00180     if (mask & IDB_LOG_OBJ_GBX) APPEND("object:gbx");
00181     if (mask & IDB_LOG_OBJ_GARBAGE) APPEND("object:garbage");
00182     if (mask & IDB_LOG_OBJ_INIT) APPEND("object:init");
00183     if (mask & IDB_LOG_OBJ_COPY) APPEND("object:copy");
00184     if (mask & IDB_LOG_EXECUTE) APPEND("execute");
00185     if (mask & IDB_LOG_DATA_READ) APPEND("data:read");
00186     if (mask & IDB_LOG_DATA_CREATE) APPEND("data:create");
00187     if (mask & IDB_LOG_DATA_WRITE) APPEND("data:write");
00188     if (mask & IDB_LOG_DATA_DELETE) APPEND("data:delete");
00189     if (mask & IDB_LOG_OQL_EXEC) APPEND("oql:exec");
00190     if (mask & IDB_LOG_OQL_RESULT) APPEND("oql:result");
00191 
00192     for (int i = 0; i < IDB_LOG_USER_MAX; i++)
00193       if (mask & IDB_LOG_USER(i)) APPEND((std::string("user:") + str_convert((long)i)).c_str());
00194 
00195     if (mask & IDB_LOG_RELSHIP) APPEND("relationship");
00196     if (mask & IDB_LOG_RELSHIP_DETAILS) APPEND("relationship:details");
00197     if (mask & IDB_LOG_SCHEMA_EVOLVE) APPEND("schema:evolve");
00198     if (mask & IDB_LOG_NOLOG) APPEND("nolog");
00199 
00200     return Success;
00201   }
00202 
00203   static LogMask
00204   getMask(const char *p, std::string &err_str)
00205   {
00206     if (!strcmp(p, "default")) return IDB_LOG_DEFAULT;
00207     if (!strcmp(p, "local")) return IDB_LOG_LOCAL;
00208     if (!strcmp(p, "server")) return IDB_LOG_SERVER;
00209     if (!strcmp(p, "connection")) return IDB_LOG_CONN;
00210     if (!strcmp(p, "transaction")) return IDB_LOG_TRANSACTION;
00211     if (!strcmp(p, "database")) return IDB_LOG_DATABASE;
00212     if (!strcmp(p, "admin")) return IDB_LOG_ADMIN;
00213     if (!strcmp(p, "exception")) return IDB_LOG_EXCEPTION;
00214     if (!strcmp(p, "oid:create")) return IDB_LOG_OID_CREATE;
00215     if (!strcmp(p, "oid:read")) return IDB_LOG_OID_READ;
00216     if (!strcmp(p, "oid:write")) return IDB_LOG_OID_WRITE;
00217     if (!strcmp(p, "oid:delete")) return IDB_LOG_OID_DELETE;
00218     if (!strcmp(p, "oid:all")) return IDB_LOG_OID_ALL;
00219     if (!strcmp(p, "memory:map")) return IDB_LOG_MMAP;
00220     if (!strcmp(p, "memory:map:detail")) return IDB_LOG_MMAP_DETAIL;
00221     if (!strcmp(p, "mutex")) return IDB_LOG_MTX;
00222     if (!strcmp(p, "index:create")) return IDB_LOG_IDX_CREATE;
00223     if (!strcmp(p, "index:remove")) return IDB_LOG_IDX_REMOVE;
00224     if (!strcmp(p, "index:insert")) return IDB_LOG_IDX_INSERT;
00225     if (!strcmp(p, "index:suppress")) return IDB_LOG_IDX_SUPPRESS;
00226     if (!strcmp(p, "index:search")) return IDB_LOG_IDX_SEARCH;
00227     if (!strcmp(p, "index:search:detail")) return IDB_LOG_IDX_SEARCH_DETAIL;
00228     if (!strcmp(p, "index:all")) return IDB_LOG_IDX_ALL;
00229     if (!strcmp(p, "dev")) return IDB_LOG_DEV;
00230     if (!strcmp(p, "object:load")) return IDB_LOG_OBJ_LOAD;
00231     if (!strcmp(p, "object:create")) return IDB_LOG_OBJ_CREATE;
00232     if (!strcmp(p, "object:update")) return IDB_LOG_OBJ_UPDATE;
00233     if (!strcmp(p, "object:remove")) return IDB_LOG_OBJ_REMOVE;
00234     if (!strcmp(p, "object:gbx")) return IDB_LOG_OBJ_GBX;
00235     if (!strcmp(p, "object:garbage")) return IDB_LOG_OBJ_GARBAGE;
00236     if (!strcmp(p, "object:init")) return IDB_LOG_OBJ_INIT;
00237     if (!strcmp(p, "object:copy")) return IDB_LOG_OBJ_COPY;
00238     if (!strcmp(p, "object:alloc")) return IDB_LOG_OBJ_ALLOC;
00239     if (!strcmp(p, "object:all")) return IDB_LOG_OBJ_ALL;
00240     if (!strcmp(p, "execute")) return IDB_LOG_EXECUTE;
00241     if (!strcmp(p, "data:read")) return IDB_LOG_DATA_READ;
00242     if (!strcmp(p, "data:create")) return IDB_LOG_DATA_CREATE;
00243     if (!strcmp(p, "data:write")) return IDB_LOG_DATA_WRITE;
00244     if (!strcmp(p, "data:delete")) return IDB_LOG_DATA_DELETE;
00245     if (!strcmp(p, "data:all")) return IDB_LOG_DATA_ALL;
00246     if (!strcmp(p, "oql:exec")) return IDB_LOG_OQL_EXEC;
00247     if (!strcmp(p, "oql:result")) return IDB_LOG_OQL_RESULT;
00248     if (!strcmp(p, "relationship")) return IDB_LOG_RELSHIP;
00249     if (!strcmp(p, "relationship:details")) return IDB_LOG_RELSHIP_DETAILS;
00250     if (!strcmp(p, "schema:evolve")) return IDB_LOG_SCHEMA_EVOLVE;
00251     if (!strcmp(p, "nolog")) return IDB_LOG_NOLOG;
00252 
00253     for (int i = 0; i < IDB_LOG_USER_MAX; i++) {
00254       std::string s = std::string("user:") + str_convert((long)i);
00255       if (!strcmp(p, s.c_str()))
00256         return IDB_LOG_USER(i);
00257     }
00258       
00259     if (err_str != std::string(""))
00260       err_str += ", ";
00261     err_str += p;
00262 
00263     return 0;
00264   }
00265 
00266   Status Log::logStringToMask(const std::string &str, LogMask &mask)
00267   {
00268     if (sscanf(str.c_str(), "%xll", &mask) == 1)
00269       return Success;
00270 
00271     std::string err_str;
00272     char *s = strdup(str.c_str());
00273     char *p = s;
00274     char *q = s;
00275     mask = 0;
00276     Bool add;
00277 
00278     if (*q == '-') {
00279       add = False;
00280       p++; q++;
00281     }
00282     else if (*q == '+') {
00283       add = True;
00284       p++; q++;
00285     }
00286     else
00287       add = True;
00288 
00289     for ( ; (q = strpbrk(q, "+-")); ) {
00290       char c = *q;
00291 
00292       *q = 0;
00293 
00294       if (add)
00295         mask |= getMask(p, err_str);
00296       else
00297         mask &= ~getMask(p, err_str);
00298 
00299       p = ++q;
00300 
00301       add = (c == '+' ? True : False);
00302     }
00303 
00304     if (add)
00305       mask |= getMask(p, err_str);
00306     else
00307       mask &= ~getMask(p, err_str);
00308 
00309     free(s);
00310 
00311     if (err_str != std::string(""))
00312       return Exception::make(IDB_ERROR, "unknown mask string(s): \"%s\".\n%s",
00313                              err_str.c_str(),
00314                              getUsage().c_str());
00315     return Success;
00316   }
00317 
00318   std::string Log::getUsage()
00319   {
00320     std::string str;
00321 
00322     str = "The logmask is an hexadecimal number or a '+/-' combination of the following strings: "
00323       "default, "
00324       "dev, "
00325       "local, "
00326       "server, "
00327       "connection, "
00328       "transaction, "
00329       "database, "
00330       "admin, "
00331       "exception, "
00332       "oid:create, "
00333       "oid:read, "
00334       "oid:write, "
00335       "oid:delete, "
00336       "oid:all, "
00337       "memory:map, "
00338       "memory:map:detail, "
00339       "mutex, "
00340       "index:create, "
00341       "index:remove, "
00342       "index:extend, "
00343       "index:insert, "
00344       "index:suppress, "
00345       "index:search, "
00346       "index:search:detail, "
00347       "index:all, "
00348       "object:load, "
00349       "object:create, "
00350       "object:update, "
00351       "object:remove, "
00352       "object:all, "
00353       "object:gbx, "
00354       "object:garbage, "
00355       "object:copy, "
00356       "object:init, "
00357       "object:alloc, "
00358       "execute, "
00359       "data:read, "
00360       "data:create, "
00361       "data:write, "
00362       "data:delete, "
00363       "data:all, "
00364       "oql:exec, "
00365       "oql:result, "
00366       "relationship, "
00367       "relationship:details, ";
00368 
00369     str += std::string("user:[0-") + str_convert((long)(IDB_LOG_USER_MAX-1)) +
00370       std::string("], ");
00371     str += "nolog.";
00372 
00373     return str;
00374   }
00375 
00376   //
00377   // C function for DBX interpreteur
00378   //
00379 
00380   void setLogMask_int(LogMask mask)
00381   {
00382     Status s = Log::setLogMask(mask);
00383     if (s) s->print();
00384   }
00385 
00386   void setLogMask_str(const char *mask)
00387   {
00388     Status s = Log::setLogMask(mask);
00389     if (s) s->print();
00390   }
00391 
00392   LogMask getLogMask()
00393   {
00394     return Log::getLogMask();
00395   }
00396 
00397   const char *getLog()
00398   {
00399     return Log::getLog();
00400   }
00401 
00402   void setLog(const char *log)
00403   {
00404     Log::setLog(log);
00405   }
00406 
00407   void resetLogTimer()
00408   {
00409     Log::resetLogTimer();
00410   }
00411 
00412   void setLogTimer(int on)
00413   {
00414     Log::setLogTimer((Bool)on);
00415   }
00416 
00417   int getLogTimer()
00418   {
00419     return (int)Log::getLogTimer();
00420   }
00421 
00422   void setLogDate(int on)
00423   {
00424     Log::setLogDate((Bool)on);
00425   }
00426 
00427   int getLogDate()
00428   {
00429     return (int)Log::getLogDate();
00430   }
00431 
00432   void setLogPid(int on)
00433   {
00434     Log::setLogPid((Bool)on);
00435   }
00436 
00437   int getLogPid()
00438   {
00439     return (int)Log::getLogPid();
00440   }
00441 
00442   void setLogProgName(int on)
00443   {
00444     Log::setLogProgName((Bool)on);
00445   }
00446 
00447   int getLogProgName()
00448   {
00449     return (int)Log::getLogProgName();
00450   }
00451 }

Generated on Mon Dec 22 18:15:58 2008 for eyedb by  doxygen 1.5.3