tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CSFLog.cpp (2216B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "CSFLog.h"
      6 
      7 #include <stdarg.h>
      8 #include <stdio.h>
      9 #include <string.h>
     10 
     11 #include "MainThreadUtils.h"
     12 #include "mozilla/Logging.h"
     13 #include "mozilla/Sprintf.h"
     14 #include "prthread.h"
     15 
     16 mozilla::LazyLogModule gSignalingLog("signaling");
     17 
     18 void CSFLogV(CSFLogLevel priority, const char* sourceFile, int sourceLine,
     19             const char* tag, const char* format, va_list args) {
     20 #ifdef STDOUT_LOGGING
     21  printf("%s\n:", tag);
     22  vprintf(format, args);
     23 #else
     24 
     25  mozilla::LogLevel level =
     26      static_cast<mozilla::LogLevel>(static_cast<unsigned int>(priority));
     27 
     28  // Skip doing any of this work if we're not logging the indicated level...
     29  if (!MOZ_LOG_TEST(gSignalingLog, level)) {
     30    return;
     31  }
     32 
     33  // Trim the path component from the filename
     34  const char* lastSlash = sourceFile;
     35  while (*sourceFile) {
     36    if (*sourceFile == '/' || *sourceFile == '\\') {
     37      lastSlash = sourceFile;
     38    }
     39    sourceFile++;
     40  }
     41  sourceFile = lastSlash;
     42  if (*sourceFile == '/' || *sourceFile == '\\') {
     43    sourceFile++;
     44  }
     45 
     46 #  define MAX_MESSAGE_LENGTH 1024
     47  char message[MAX_MESSAGE_LENGTH];
     48 
     49  const char* threadName = NULL;
     50 
     51  // Check if we're the main thread...
     52  if (NS_IsMainThread()) {
     53    threadName = "main";
     54  } else {
     55    threadName = PR_GetThreadName(PR_GetCurrentThread());
     56  }
     57 
     58  // If we can't find it anywhere, use a blank string
     59  if (!threadName) {
     60    threadName = "";
     61  }
     62 
     63  VsprintfLiteral(message, format, args);
     64  MOZ_LOG(
     65      gSignalingLog, level,
     66      ("[%s|%s] %s:%d: %s", threadName, tag, sourceFile, sourceLine, message));
     67 #endif
     68 }
     69 
     70 void CSFLog(CSFLogLevel priority, const char* sourceFile, int sourceLine,
     71            const char* tag, const char* format, ...) {
     72  va_list ap;
     73  va_start(ap, format);
     74 
     75  CSFLogV(priority, sourceFile, sourceLine, tag, format, ap);
     76  va_end(ap);
     77 }
     78 
     79 int CSFLogTestLevel(CSFLogLevel priority) {
     80  return MOZ_LOG_TEST(gSignalingLog, static_cast<mozilla::LogLevel>(
     81                                         static_cast<unsigned int>(priority)));
     82 }