vlog.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/vlog.h"
6
7#include "base/basictypes.h"
8#include "base/string_number_conversions.h"
9#include "base/string_split.h"
10#include "base/string_util.h"
11
12namespace logging {
13
14const int VlogInfo::kDefaultVlogLevel = 0;
15
16VlogInfo::VlogInfo(const std::string& v_switch,
17                   const std::string& vmodule_switch)
18    : max_vlog_level_(kDefaultVlogLevel) {
19  typedef std::pair<std::string, std::string> KVPair;
20  if (!base::StringToInt(v_switch, &max_vlog_level_)) {
21    LOG(WARNING) << "Parsed v switch \""
22                 << v_switch << "\" as " << max_vlog_level_;
23  }
24  std::vector<KVPair> kv_pairs;
25  if (!base::SplitStringIntoKeyValuePairs(
26          vmodule_switch, '=', ',', &kv_pairs)) {
27    LOG(WARNING) << "Could not fully parse vmodule switch \""
28                 << vmodule_switch << "\"";
29  }
30  for (std::vector<KVPair>::const_iterator it = kv_pairs.begin();
31       it != kv_pairs.end(); ++it) {
32    int vlog_level = kDefaultVlogLevel;
33    if (!base::StringToInt(it->second, &vlog_level)) {
34      LOG(WARNING) << "Parsed vlog level for \""
35                   << it->first << "=" << it->second
36                   << "\" as " << vlog_level;
37    }
38    vmodule_levels_.push_back(std::make_pair(it->first, vlog_level));
39  }
40}
41
42VlogInfo::~VlogInfo() {}
43
44int VlogInfo::GetVlogLevel(const base::StringPiece& file) {
45  if (!vmodule_levels_.empty()) {
46    base::StringPiece module(file);
47    base::StringPiece::size_type last_slash_pos =
48        module.find_last_of("\\/");
49    if (last_slash_pos != base::StringPiece::npos) {
50      module.remove_prefix(last_slash_pos + 1);
51    }
52    base::StringPiece::size_type extension_start = module.find('.');
53    module = module.substr(0, extension_start);
54    static const char kInlSuffix[] = "-inl";
55    static const int kInlSuffixLen = arraysize(kInlSuffix) - 1;
56    if (module.ends_with(kInlSuffix)) {
57      module.remove_suffix(kInlSuffixLen);
58    }
59    for (std::vector<VmodulePattern>::const_iterator it =
60             vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) {
61      // TODO(akalin): Use a less-heavyweight version of MatchPattern
62      // (we can pretty much assume we're dealing with ASCII).
63      if (MatchPattern(module, it->first)) {
64        return it->second;
65      }
66    }
67  }
68  return max_vlog_level_;
69}
70
71}  // namespace
72