Add ring logging for interesting debugging events
[hiphop-php.git] / hphp / util / trace.cpp
blob353b49acfef684528ad7c99df379fef9052cf41b
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
18 * Forcibly define USE_TRACE, so we get the debug trace.h interface included
19 * here. This allows mixed compilation, where some units were compiled
20 * DEBUG and others compiled RELEASE, to successfully link.
22 #ifndef USE_TRACE
23 # define USE_TRACE 1
24 #endif
25 #include "hphp/util/trace.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string>
33 #include "hphp/util/ringbuffer.h"
35 namespace HPHP {
37 static const Trace::Module TRACEMOD = Trace::tprefix;
39 namespace Trace {
41 int levels[NumModules];
42 static FILE* out;
44 const char *tokNames[] = {
45 #define TM(x) #x,
46 TRACE_MODULES
47 #undef TM
51 * Dummy class to get some code to run before main().
53 class Init {
54 Module name2mod(const char *name) {
55 for (int i = 0; i < NumModules; i++) {
56 if (!strcasecmp(tokNames[i], name)) {
57 return (Module)i;
60 return (Module)-1;
63 public:
64 Init() {
65 /* Parse the environment for flags. */
66 const char *envName = "TRACE";
67 const char *env = getenv(envName);
68 const char *file = getenv("HPHP_TRACE_FILE");
69 if (!file) file = "/tmp/hphp.log";
70 if (env) {
71 out = fopen(file, "w");
72 if (!out) {
73 fprintf(stderr, "could not create log file (%s); using stderr\n", file);
74 out = stderr;
76 char *e = strdup(env);
77 char *tok;
78 for (tok = strtok(e, ","); tok; tok = strtok(nullptr, ",")) {
79 char *ctok;
80 char *moduleName = tok;
81 if (( ctok = strchr(moduleName, ':'))) {
82 *ctok++ = 0;
84 int level = ctok ? atoi(ctok) : 1;
85 int mod = name2mod(moduleName);
86 if (mod >= 0) {
87 levels[mod] = level;
89 if (mod == Trace::minstr || mod == Trace::punt) {
90 levels[Trace::statgroups] = std::max(levels[Trace::statgroups], 1);
93 free(e);
94 } else {
95 // If TRACE env var is not set, nothing should be traced...
96 // but if it does, use stderr.
97 out = stderr;
102 Init i;
104 const char* moduleName(Module mod) {
105 return tokNames[mod];
108 void flush() {
109 if (!moduleEnabledRelease(Trace::traceAsync)) {
110 fflush(out);
114 void vtrace(const char *fmt, va_list ap) {
115 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
116 if (moduleEnabledRelease(Trace::ringbuffer, 1)) {
117 vtraceRingbuffer(fmt, ap);
118 } else {
119 ONTRACE(1, pthread_mutex_lock(&mtx));
120 ONTRACE(1, fprintf(out, "t%#08x: ",
121 int((int64_t)pthread_self() & 0xFFFFFFFF)));
122 vfprintf(out, fmt, ap);
123 ONTRACE(1, pthread_mutex_unlock(&mtx));
124 flush();
128 void trace(const char *fmt, ...) {
129 va_list ap;
130 va_start(ap, fmt);
131 vtrace(fmt, ap);
132 va_end(ap);
135 void traceRelease(const char* fmt, ...) {
136 va_list ap;
137 va_start(ap, fmt);
138 vtrace(fmt, ap);
139 va_end(ap);
142 void traceRingBufferRelease(const char *fmt, ...) {
143 va_list ap;
144 va_start(ap, fmt);
145 vtraceRingbuffer(fmt, ap);
146 va_end(ap);
149 void trace(const std::string& s) {
150 trace("%s", s.c_str());
153 void traceRelease(const std::string& s) {
154 traceRelease("%s", s.c_str());
157 template<>
158 std::string prettyNode(const char* name, const std::string& s) {
159 using std::string;
160 return string("(") + string(name) + string(" ") +
162 string(")");
165 } } // HPHP::Trace