1 // Copyright (C) 2010-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
18
19 namespace sipwitch {
20
21 static mutex_t histlock;
22 static OrderedIndex histindex;
23 static unsigned histcount = 0;
24 static unsigned histlimit = 0;
25
28 {
29 ++histcount;
31 }
32
34 {
35 Time now;
36 char buf[20];
37
38 now.put(buf);
39 snprintf(
text,
sizeof(
text),
"%s %02d %s",
40 buf, (int)lid, msg);
41
42 char *cp = strchr(
text,
'\n');
43 if(cp)
44 *cp = 0;
45 }
46
48 {
50
51 // if no logging active, nothing to add...
52 if(!histlimit)
53 return;
54
55 histlock.acquire();
56 // if not to buffer limit, start by allocating
57 // maybe we could use a pager heap....
58 if(histcount < histlimit) {
60 histlock.release();
61 return;
62 }
63
64 reuse = (
history *)histindex.begin();
65 reuse->delist(&histindex);
67 reuse->enlist(&histindex);
68 histlock.release();
69 }
70
72 {
74
75 histlock.acquire();
76 while(histcount > limit) {
77 reuse = (
history *)histindex.begin();
78 reuse->delist(&histindex);
79 delete reuse;
80 }
81 histlimit = limit;
82 histlock.release();
83 }
84
86 {
87 if(!histlimit)
88 return;
89
91
92 if(!fp)
93 return;
94
95 histlock.acquire();
96 linked_pointer<history> hp = histindex.begin();
97 while(is(hp)) {
98 fprintf(fp, "%s\n", hp->text);
99 hp.next();
100 }
101 histlock.release();
102 fclose(fp);
103 }
104
105 } // end namespace
106
history(shell::loglevel_t lid, const char *msg)
static void add(shell::loglevel_t lid, const char *msg)
static bool static FILE * output(const char *id)
Used to open an output session for returning control data.
void set(shell::loglevel_t lid, const char *msg)