common/omdebug.cc

Go to the documentation of this file.
00001 /* omdebug.cc: Debugging class
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002 Ananova Ltd
00005  * Copyright 2003,2004,2005,2006,2008 Olly Betts
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #include <config.h>
00024 
00025 #include "omdebug.h"
00026 
00027 #ifdef XAPIAN_DEBUG_VERBOSE
00028 
00029 #include "utils.h"
00030 
00031 OmDebug om_debug;
00032 
00033 #include <stdlib.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include "safefcntl.h"
00037 #include "safeerrno.h"
00038 
00039 #include <string>
00040 
00041 using namespace std;
00042 
00043 #define ENV_DEBUG_LOG   "XAPIAN_DEBUG_LOG"
00044 #define ENV_DEBUG_FLAGS "XAPIAN_DEBUG_FLAGS"
00045 
00046 OmDebug::OmDebug() : initialised(false), wanted_types(0), fd(2)
00047 {
00048     // Can't do much in this constructor, because on Solaris the contents get
00049     // wiped just before the start of main().
00050 }
00051 
00052 OmDebug::~OmDebug()
00053 {
00054     display_message(OM_DEBUG_UNKNOWN, "Xapian debug version, closing down\n");
00055     initialised = false;
00056 }
00057 
00058 void
00059 OmDebug::initialise()
00060 {
00061     if (!initialised) {
00062         initialised = true;
00063         // We get this as soon as we can - possible race condition exists here
00064         // if the initialise() method is not explicitly called.
00065         const char * typestring = getenv(ENV_DEBUG_FLAGS);
00066         if (typestring) wanted_types = atoi(typestring);
00067 
00068         const char * filename = getenv(ENV_DEBUG_LOG);
00069         if (filename) {
00070             string s(filename);
00071             string::size_type token = s.find("%%");
00072             if (token != string::npos) {
00073                 s.replace(token, 2, om_tostring(getpid()));
00074             }
00075 
00076             // mingw doesn't support O_SYNC, and it's not vital - it just
00077             // ensures that debug output is written to disk, so that none
00078             // is lost if we crash...
00079 #ifdef O_SYNC
00080             fd = open(s.c_str(), O_CREAT | O_WRONLY | O_SYNC | O_APPEND, 0644);
00081 #else
00082             fd = open(s.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
00083 #endif
00084 
00085             if (fd == -1) {
00086                 fd = 2;
00087                 display_message(OM_DEBUG_UNKNOWN, "Can't open requested debug log `" + s + "' - using stderr.\n");
00088             }
00089         }
00090 
00091         display_message(OM_DEBUG_UNKNOWN, "Xapian debug build initialised\n");
00092     }
00093 }
00094 
00095 bool
00096 OmDebug::want_type(enum om_debug_types type)
00097 {
00098     initialise();
00099     return (wanted_types >> type) & 1;
00100 }
00101 
00102 void
00103 OmDebug::display_message(enum om_debug_types type, string msg)
00104 {
00105     if (!want_type(type)) return;
00106     string line(om_tostring(int(type)));
00107     line += ']';
00108     line += msg;
00109     size_t written = 0;
00110     while (written < line.size()) {
00111         ssize_t n = write(fd, line.data() + written, line.size() - written);
00112         if (n < 0) {
00113             if (errno == EINTR)
00114                 continue;
00115             return; // Ignore errors
00116         }
00117         written += n;
00118     }
00119 }
00120 
00121 #endif /* XAPIAN_DEBUG_VERBOSE */
00122 
00123 #ifdef XAPIAN_DEBUG_PROFILE
00124 
00125 struct timeval Xapian::Internal::Timer::paused;
00126 
00127 struct timeval * Xapian::Internal::Timer::pstart = NULL;
00128 
00129 std::list<Xapian::Internal::Timer *> Xapian::Internal::Timer::stack;
00130 
00131 int Xapian::Internal::Timer::depth = 0;
00132 
00133 #endif

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.