00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00049
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
00064
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
00077
00078
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;
00116 }
00117 written += n;
00118 }
00119 }
00120
00121 #endif
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