File: logs.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (191 lines) | stat: -rw-r--r-- 6,060 bytes parent folder | download | duplicates (5)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Copyright (©) 2003-2025 Teus Benschop.

This program 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 3 of the License, or
(at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include <database/logs.h>
#include <filter/url.h>
#include <filter/string.h>
#include <config/globals.h>
#include <database/sqlite.h>
#include <filter/date.h>
#include <journal/logic.h>


// Bibledit no longer uses a database for storing the journal.
// Reasons that a database is no longer used:
// 1. Simpler system.
// 2. Android has VACUUM errors due to a locked database.


// Records a journal entry.
void Database_Logs::log (std::string description, int level)
{
  // Trim spaces.
  description = filter::strings::trim (description);
  // Discard empty line.
  if (description.empty()) return;
  // Truncate very long entry.
  size_t length = description.length ();
  if (length > 50000) {
    description.erase (50000);
    description.append ("... This entry was too large and has been truncated: " + std::to_string (length) + " bytes");
  }
  // Save this logbook entry to a filename with seconds and microseconds.
  std::string seconds = std::to_string (filter::date::seconds_since_epoch ());
  std::string time = seconds + filter::strings::fill (std::to_string (filter::date::numerical_microseconds ()), 8, '0');
  std::string file = filter_url_create_path ({folder (), time});
  // The microseconds granularity depends on the platform.
  // On Windows it is lower than on Linux.
  // There may be the rare case of more than one entry per file.
  // Append the data so it won't overwrite an earlier entry.
  if (file_or_dir_exists (file)) {
    description.insert (0, " | ");
  } else {
    description.insert (0, std::to_string (level) + " ");
  }
  filter_url_file_put_contents_append (file, description);
#ifdef HAVE_WINDOWS
  // Delay to cover for lower usec granularity on Windows.
  std::this_thread::sleep_for (std::chrono::milliseconds (1));
#endif
}


// Records an extended journal entry.
void Database_Logs::log (std::string subject, std::string body, int level)
{
  std::string description (subject);
  description.append ("\n");
  description.append (body);
  log (description, level);
}


void Database_Logs::rotate ()
{
  // Under PHP it used a mechanism that handled huge amounts of entries.
  // The PHP function scandir choked on this or took a very long time.
  // The PHP functions opendir / readdir / closedir handled it better.
  // But now, in C++, with the new journal mechanism, this is no longer relevant.
  std::string directory = folder ();
  std::vector <std::string> files = filter_url_scandir (directory);

  
  // Timestamp for removing older records, depending on whether it's a tiny journal.
#ifdef HAVE_TINY_JOURNAL
  int oldtimestamp = filter::date::seconds_since_epoch () - (14400);
#else
  int oldtimestamp = filter::date::seconds_since_epoch () - (6 * 86400);
#endif

  
  // Limit the journal entry count in the filesystem.
  // This speeds up subsequent reading of the journal by the users.
  // In previous versions of Bibledit, there were certain conditions
  // that led to an infinite loop, as had been noticed at times,
  // and this quickly exhausted the available inodes on the filesystem.
#ifdef HAVE_TINY_JOURNAL
  const int limitfilecount = static_cast<int>(files.size () - 200);
#else
  const int limitfilecount = static_cast<int>(files.size () - 2000);
#endif

  
  bool filtered_entries = false;
  for (unsigned int i = 0; i < files.size(); i++) {
    std::string path = filter_url_create_path ({directory, files [i]});

    // Limit the number of journal entries.
    if (static_cast<int> (i) < limitfilecount) {
      filter_url_unlink (path);
      continue;
    }
    
    // Remove expired entries.
    int timestamp = filter::strings::convert_to_int (files [i].substr (0, 10));
    if (timestamp < oldtimestamp) {
      filter_url_unlink (path);
      continue;
    }

    // Filtering of certain entries.
    std::string entry = filter_url_file_get_contents (path);
    if (journal_logic_filter_entry (entry)) {
      filtered_entries = true;
      filter_url_unlink (path);
      continue;
    }

  }

  if (filtered_entries) {
    log (journal_logic_filtered_message ());
  }
}


// Get the logbook entries.
std::vector <std::string> Database_Logs::get (std::string & lastfilename)
{
  lastfilename = "0";

  // Read the journal records from the filesystem.
  std::vector <std::string> files = filter_url_scandir (folder ());
  for (unsigned int i = 0; i < files.size(); i++) {
    // Last second gets updated based on the filename.
    lastfilename = files [i];
  }

  // Done.  
  return files;
}


// Gets journal entry more recent than "filename".
// Updates "filename" to the item it got.
std::string Database_Logs::next (std::string &filename)
{
  std::vector <std::string> files = filter_url_scandir (folder ());
  for (unsigned int i = 0; i < files.size (); i++) {
    std::string file = files [i];
    if (file > filename) {
      filename = file;
      return file;
    }
  }
  return std::string();
}


// Clears all journal entries.
void Database_Logs::clear ()
{
  std::string directory = folder ();
  std::vector <std::string> files = filter_url_scandir (directory);
  for (auto file : files) {
    filter_url_unlink (filter_url_create_path ({directory, file}));
  }
  log ("The journal was cleared");
}


// The folder where to store the records.
std::string Database_Logs::folder ()
{
  return filter_url_create_root_path ({"logbook"});
}