2 +----------------------------------------------------------------------+
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.
25 #include "hphp/util/trace.h"
33 #include "hphp/util/ringbuffer.h"
37 static const Trace::Module TRACEMOD
= Trace::tprefix
;
41 int levels
[NumModules
];
44 const char *tokNames
[] = {
51 * Dummy class to get some code to run before main().
54 Module
name2mod(const char *name
) {
55 for (int i
= 0; i
< NumModules
; i
++) {
56 if (!strcasecmp(tokNames
[i
], name
)) {
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";
71 out
= fopen(file
, "w");
73 fprintf(stderr
, "could not create log file (%s); using stderr\n", file
);
76 char *e
= strdup(env
);
78 for (tok
= strtok(e
, ","); tok
; tok
= strtok(nullptr, ",")) {
80 char *moduleName
= tok
;
81 if (( ctok
= strchr(moduleName
, ':'))) {
84 int level
= ctok
? atoi(ctok
) : 1;
85 int mod
= name2mod(moduleName
);
89 if (mod
== Trace::minstr
|| mod
== Trace::punt
) {
90 levels
[Trace::statgroups
] = std::max(levels
[Trace::statgroups
], 1);
95 // If TRACE env var is not set, nothing should be traced...
96 // but if it does, use stderr.
104 const char* moduleName(Module mod
) {
105 return tokNames
[mod
];
109 if (!moduleEnabledRelease(Trace::traceAsync
)) {
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
);
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
));
128 void trace(const char *fmt
, ...) {
135 void traceRelease(const char* fmt
, ...) {
142 void traceRingBufferRelease(const char *fmt
, ...) {
145 vtraceRingbuffer(fmt
, 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());
158 std::string
prettyNode(const char* name
, const std::string
& s
) {
160 return string("(") + string(name
) + string(" ") +