SIP Witch 1.9.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
history.cpp
Go to the documentation of this file.
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 
17 #include "server.h"
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 
26 history::history(shell::loglevel_t lid, const char *msg) :
27 OrderedObject(&histindex)
28 {
29  ++histcount;
30  set(lid, msg);
31 }
32 
33 void history::set(shell::loglevel_t lid, const char *msg)
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 
47 void history::add(shell::loglevel_t lid, const char *msg)
48 {
49  history *reuse;
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) {
59  new history(lid, msg);
60  histlock.release();
61  return;
62  }
63 
64  reuse = (history *)histindex.begin();
65  reuse->delist(&histindex);
66  reuse->set(lid, msg);
67  reuse->enlist(&histindex);
68  histlock.release();
69 }
70 
71 void history::set(unsigned limit)
72 {
73  history *reuse;
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 
85 void history::out(void)
86 {
87  if(!histlimit)
88  return;
89 
90  FILE *fp = control::output("history");
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 
static void out(void)
Definition: history.cpp:85
char text[128]
Definition: server.h:757
history(shell::loglevel_t lid, const char *msg)
Definition: history.cpp:26
static void add(shell::loglevel_t lid, const char *msg)
Definition: history.cpp:47
static bool static FILE * output(const char *id)
Used to open an output session for returning control data.
Definition: control.cpp:380
void set(shell::loglevel_t lid, const char *msg)
Definition: history.cpp:33