<commit_before>/*
 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * SRPC utility functions.
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "native_client/src/include/nacl_assert.h"
#include "native_client/src/include/portability.h"
#include "native_client/src/include/portability_io.h"
#include "native_client/src/include/portability_process.h"

#include "native_client/src/shared/platform/nacl_sync_checked.h"
#include "native_client/src/shared/platform/nacl_threads.h"
#include "native_client/src/shared/platform/nacl_timestamp.h"
#include "native_client/src/shared/srpc/nacl_srpc.h"
#include "native_client/src/shared/srpc/nacl_srpc_internal.h"

/* TODO(sehr): Re-enable logging mutex when this does not require C++. */
/* static struct NaClMutex log_mu; */

#define VERBOSITY_NOT_SET ((int) (((unsigned) -1) >> 1))

static int verbosity = VERBOSITY_NOT_SET;

int NaClSrpcLogInit() {
  /* NaClXMutexCtor(&log_mu); */
  return 1;
}

void NaClSrpcLogFini() {
  /* NaClMutexDtor(&log_mu); */
}

static int getVerbosity() {
  if (VERBOSITY_NOT_SET == verbosity) {
    const char* env_verbosity = getenv("NACL_SRPC_DEBUG");
    verbosity = 0;
    if (NULL != env_verbosity) {
      int v = strtol(env_verbosity, (char**) 0, 0);
      if (v >= 0) {
        verbosity = v;
      }
    }
  }
  return verbosity;
}

void NaClSrpcLog(int detail_level, const char* fmt, ...) {
  /*
   * It would be better to use the NaClLog infrastructure to do this, but
   * it requires some more enhancement to be ready.
   * http://code.google.com/p/nativeclient/issues/detail?id=1802
   * TODO(bsy,sehr): convert this when NaClLog is ready for SRPC levels, etc.
   */
  if (detail_level <= getVerbosity()) {
    int pid = GETPID();
    char timestamp[128];
    va_list ap;
#ifdef __native_client__
    const char* host_or_nacl = "NACL";
#else
    const char* host_or_nacl = "HOST";
#endif
    va_start(ap, fmt);
    /* NaClXMutexLock(&log_mu); */
    fprintf(stderr,
            "[SRPC:%s:%d:%s] ",
            host_or_nacl,
            pid,
            NaClTimeStampString(timestamp, sizeof timestamp));
    vfprintf(stderr, fmt, ap);
    /* NaClXMutexUnlock(&log_mu); */
    va_end(ap);
  }
}

/*
 * This makes a best-effort escape sequence to feed sel_universal's parser
 * in src/trusted/sel_universal/parsing.cc.
 */
static int shouldPrintAsEscaped(int cval) {
  switch (cval) {
    case '\0':
      return '0';
    case '\\':
      return '\\';<commit_msg>Fix missing symbol for NaClTimeStampString. BUG=none TEST=pnacl sandboxed translator build Review URL: http://codereview.chromium.org/7010014<commit_after>/*
 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * SRPC utility functions.
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "native_client/src/include/nacl_assert.h"
#include "native_client/src/include/portability.h"
#include "native_client/src/include/portability_io.h"
#include "native_client/src/include/portability_process.h"

#include "native_client/src/shared/platform/nacl_sync_checked.h"
#include "native_client/src/shared/platform/nacl_threads.h"
#include "native_client/src/shared/platform/nacl_timestamp.h"
#include "native_client/src/shared/srpc/nacl_srpc.h"
#include "native_client/src/shared/srpc/nacl_srpc_internal.h"

/* TODO(sehr): Re-enable logging mutex when this does not require C++. */
/* static struct NaClMutex log_mu; */

#define VERBOSITY_NOT_SET ((int) (((unsigned) -1) >> 1))

static int verbosity = VERBOSITY_NOT_SET;

int NaClSrpcLogInit() {
  /* NaClXMutexCtor(&log_mu); */
  return 1;
}

void NaClSrpcLogFini() {
  /* NaClMutexDtor(&log_mu); */
}

static int getVerbosity() {
  if (VERBOSITY_NOT_SET == verbosity) {
    const char* env_verbosity = getenv("NACL_SRPC_DEBUG");
    verbosity = 0;
    if (NULL != env_verbosity) {
      int v = strtol(env_verbosity, (char**) 0, 0);
      if (v >= 0) {
        verbosity = v;
      }
    }
  }
  return verbosity;
}

void NaClSrpcLog(int detail_level, const char* fmt, ...) {
  /*
   * It would be better to use the NaClLog infrastructure to do this, but
   * it requires some more enhancement to be ready.
   * http://code.google.com/p/nativeclient/issues/detail?id=1802
   * TODO(bsy,sehr): convert this when NaClLog is ready for SRPC levels, etc.
   */
  if (detail_level <= getVerbosity()) {
    int pid = GETPID();
    va_list ap;
#ifdef __native_client__
    const char* host_or_nacl = "NACL";
#else
    const char* host_or_nacl = "HOST";
#endif
    va_start(ap, fmt);
    /* NaClXMutexLock(&log_mu); */
    fprintf(stderr,
            "[SRPC:%s:%d] ",
            host_or_nacl,
            pid);
    vfprintf(stderr, fmt, ap);
    /* NaClXMutexUnlock(&log_mu); */
    va_end(ap);
  }
}

/*
 * This makes a best-effort escape sequence to feed sel_universal's parser
 * in src/trusted/sel_universal/parsing.cc.
 */
static int shouldPrintAsEscaped(int cval) {
  switch (cval) {
    case '\0':
      return '0';
    case '\\':
      return '\\';
{'fraction_non_alphanumeric': 0.13521180030257185, 'fraction_numerical': 0.00832072617246596, 'mean_word_length': 3.497448979591837, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5868903', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1858, 'n_words': 445}