-
Notifications
You must be signed in to change notification settings - Fork 5
/
stats.cc
70 lines (56 loc) · 1.84 KB
/
stats.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright (C) 2008 Nicolas Vion <nico@picapo.net>
*
* This file is part of Topy.
*
* Topy is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Topy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Topy; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _STATS_CC
#include "stats.hh"
#include <sstream>
void Stats::set(std::string const field, Counter const value) {
mutex.lock();
List::iterator it = list.find(field);
if (it != list.end())
it->second = value;
else
list.insert(std::pair<std::string, Counter> (field, value));
mutex.unlock();
}
void Stats::inc(std::string const field) {
mutex.lock();
List::iterator it = list.find(field);
if (it != list.end())
it->second++;
else
list.insert(std::pair<std::string, Counter> (field, 1));
mutex.unlock();
}
void Stats::show(std::stringstream &out, std::string const prefix) {
mutex.lock();
for (List::iterator it = list.begin(); it != list.end(); it++) {
out << "STAT " << prefix << it->first << " " << it->second << "\n";
}
mutex.unlock();
}
void Stats::serialize_php(std::stringstream &out) {
mutex.lock();
out << "a:" << list.size() << ":{";
for (List::iterator it = list.begin(); it != list.end(); it++) {
out << "s:" << it->first.size() << ":\"" << it->first << "\";i:" << it->second << ";";
}
out << "}";
mutex.unlock();
}