vlog.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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
42int VlogInfo::GetVlogLevel(const base::StringPiece& file) {
43  if (!vmodule_levels_.empty()) {
44    base::StringPiece module(file);
45    base::StringPiece::size_type last_slash_pos =
46        module.find_last_of("\\/");
47    if (last_slash_pos != base::StringPiece::npos) {
48      module.remove_prefix(last_slash_pos + 1);
49    }
50    base::StringPiece::size_type extension_start = module.find('.');
51    module = module.substr(0, extension_start);
52    static const char kInlSuffix[] = "-inl";
53    static const int kInlSuffixLen = arraysize(kInlSuffix) - 1;
54    if (module.ends_with(kInlSuffix)) {
55      module.remove_suffix(kInlSuffixLen);
56    }
57    for (std::vector<VmodulePattern>::const_iterator it =
58             vmodule_levels_.begin(); it != vmodule_levels_.end(); ++it) {
59      // TODO(akalin): Use a less-heavyweight version of MatchPattern
60      // (we can pretty much assume we're dealing with ASCII).
61      if (MatchPattern(module, it->first)) {
62        return it->second;
63      }
64    }
65  }
66  return max_vlog_level_;
67}
68
69}  // namespace
70