plugin - read_log / log_history - fix corner cases so behavior matches documentation

This commit is contained in:
Ken Patel 2011-02-12 02:10:23 +00:00
parent bf5343ed17
commit 39967c1110
2 changed files with 72 additions and 47 deletions

View File

@ -546,48 +546,54 @@ read_log(const string &log_pathname, P3D_object *params[], int num_params) {
if (num_params > 3) {
head_bytes_prev = (size_t)max(P3D_OBJECT_GET_INT(params[3]), 0);
}
// Read the log data from the primary file
read_log_file(log_pathname, tail_bytes, head_bytes, log_data);
// Read data from previous logs if requested
if ((tail_bytes_prev > 0) || (head_bytes_prev > 0)) {
// Determine the base of the log file names
string log_basename = log_pathname;
size_t slash = log_basename.rfind('/');
if (slash != string::npos) {
log_basename = log_basename.substr(slash + 1);
}
#ifdef _WIN32
slash = log_basename.rfind('\\');
if (slash != string::npos) {
log_basename = log_basename.substr(slash + 1);
}
#endif // _WIN32
string log_basename_primary = log_basename;
int dash = log_basename.rfind("-");
if (dash != string::npos) {
log_basename = log_basename.substr(0, dash+1);
} else {
int dotLog = log_basename.rfind(".log");
if (dotLog != string::npos) {
log_basename = log_basename.substr(0, dotLog);
log_basename += "-";
}
}
// Find matching files
vector<string> all_logs;
inst_mgr->scan_directory(log_directory, all_logs);
for (int i=(int)all_logs.size()-1; i>=0; --i) {
if ((all_logs[i].find(log_basename) == 0) &&
(all_logs[i] != log_basename_primary) &&
(all_logs[i].size() > 4) &&
(all_logs[i].substr(all_logs[i].size() - 4) == string(".log"))) {
read_log_file((log_directory+all_logs[i]), tail_bytes_prev, head_bytes_prev, log_data);
}
// Determine the base of the log file names
nout << "log_pathname: " << log_pathname << "\n";
string log_basename = log_pathname;
size_t slash = log_basename.rfind('/');
if (slash != string::npos) {
log_basename = log_basename.substr(slash + 1);
}
#ifdef _WIN32
slash = log_basename.rfind('\\');
if (slash != string::npos) {
log_basename = log_basename.substr(slash + 1);
}
#endif // _WIN32
string log_leafname_primary = log_basename;
int dash = log_basename.rfind("-");
if (dash != string::npos) {
log_basename = log_basename.substr(0, dash+1);
} else {
int dotLog = log_basename.rfind(".log");
if (dotLog != string::npos) {
log_basename = log_basename.substr(0, dotLog);
log_basename += "-";
}
}
// Read matching files
vector<string> all_logs;
int log_matches_found = 0;
string log_matching_pathname;
inst_mgr->scan_directory(log_directory, all_logs);
for (int i=(int)all_logs.size()-1; i>=0; --i) {
if ((all_logs[i] == log_leafname_primary) ||
(all_logs[i].find(log_basename) == 0) &&
(all_logs[i].size() > 4) &&
(all_logs[i].substr(all_logs[i].size() - 4) == string(".log"))) {
log_matches_found++;
log_matching_pathname = (log_directory + all_logs[i]);
read_log_file(log_matching_pathname, tail_bytes, head_bytes, log_data);
tail_bytes = tail_bytes_prev;
head_bytes = head_bytes_prev;
}
}
if (log_matches_found == 0) {
nout << "read_log: warning: no matching file(s) on disk." << "\n";
}
string log_data_str = log_data.str();
P3D_object *result = new P3DStringObject(log_data_str);
return result;

View File

@ -1054,31 +1054,49 @@ start_p3dpython(P3DInstance *inst) {
}
#endif // _WIN32
// Check if we want to keep copies of recent logs on disk.
// Get the log history count from the HTML tokens, or from the
// p3d_info.xml file.
int log_history = inst->get_fparams().lookup_token_int("log_history");
if (!log_basename.empty() && (log_history > 1)) {
// Append suffix separator
log_basename += "-";
// Delete all but the most recent 'log_history' logs
// Check if we want to keep copies of recent logs on disk.
if (!log_basename.empty()) {
// Get a list of all logs on disk
vector<string> all_logs;
vector<string> matching_logs;
string log_directory = inst_mgr->get_log_directory();
inst_mgr->scan_directory(log_directory, all_logs);
// If keeping logs, only logs with a -timestamp suffix are valid.
if (log_history > 0) {
// Remove exact match (no suffix) file, if it is on disk
string log_exact_leafname = (log_basename + string(".log"));
for (int i=0; i<(int)all_logs.size(); ++i) {
if (all_logs[i] == log_exact_leafname) {
string log_exact_pathname = (log_directory + log_exact_leafname);
unlink(log_exact_pathname.c_str());
break;
}
}
}
// Remove all but the most recent log_history timestamped logs
string log_basename_dash = (log_basename + string("-"));
string log_matching_pathname;
vector<string> matching_logs;
for (int i=0; i<(int)all_logs.size(); ++i) {
if ((all_logs[i].size() > 4) &&
(all_logs[i].find(log_basename) == 0) &&
(all_logs[i].find(log_basename_dash) == 0) &&
(all_logs[i].substr(all_logs[i].size() - 4) == string(".log"))) {
matching_logs.push_back((log_directory + all_logs[i]));
log_matching_pathname = (log_directory + all_logs[i]);
matching_logs.push_back(log_matching_pathname);
}
}
for (int i=0; i<(int)matching_logs.size()-log_history; ++i) {
unlink(matching_logs[i].c_str());
}
}
// Append a timestamp suffix to the log_basename
// Append a timestamp suffix to the log_basename
if (!log_basename.empty() && (log_history > 0)) {
#ifdef _WIN32
_tzset();
#else
@ -1097,6 +1115,7 @@ start_p3dpython(P3DInstance *inst) {
(int)(log_time_local.tm_hour),
(int)(log_time_local.tm_min),
(int)(log_time_local.tm_sec));
log_basename += "-";
log_basename += buffer;
}
}