mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Log: Added logging support
This commit is contained in:
parent
a0c95edfcd
commit
3a751e628a
5 changed files with 46 additions and 17 deletions
|
@ -25,12 +25,14 @@
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <cmake.h>
|
#include <cmake.h>
|
||||||
|
#include <Log.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int CmdLog ()
|
int CmdLog (Log& log)
|
||||||
{
|
{
|
||||||
std::cout << "# log\n";
|
std::cout << "# log\n";
|
||||||
|
log.write ("debug", "----8<----");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#define INCLUDED_COMMANDS
|
#define INCLUDED_COMMANDS
|
||||||
|
|
||||||
#include <Rules.h>
|
#include <Rules.h>
|
||||||
|
#include <Log.h>
|
||||||
|
|
||||||
int CmdClear ();
|
int CmdClear ();
|
||||||
int CmdConfig ();
|
int CmdConfig ();
|
||||||
|
@ -40,7 +41,7 @@ int CmdExtension ();
|
||||||
int CmdGaps ();
|
int CmdGaps ();
|
||||||
int CmdHelpUsage ();
|
int CmdHelpUsage ();
|
||||||
int CmdHelp ();
|
int CmdHelp ();
|
||||||
int CmdLog ();
|
int CmdLog (Log&);
|
||||||
int CmdImport ();
|
int CmdImport ();
|
||||||
int CmdReport ();
|
int CmdReport ();
|
||||||
int CmdStart ();
|
int CmdStart ();
|
||||||
|
|
24
src/init.cpp
24
src/init.cpp
|
@ -29,6 +29,7 @@
|
||||||
#include <Database.h>
|
#include <Database.h>
|
||||||
#include <Rules.h>
|
#include <Rules.h>
|
||||||
#include <Extensions.h>
|
#include <Extensions.h>
|
||||||
|
#include <Log.h>
|
||||||
//#include <Grammar.h>
|
//#include <Grammar.h>
|
||||||
//#include <LR0.h>
|
//#include <LR0.h>
|
||||||
#include <shared.h>
|
#include <shared.h>
|
||||||
|
@ -53,7 +54,10 @@ bool lightweightVersionCheck (int argc, const char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void initializeData (Configuration& configuration, Database& database)
|
void initializeData (
|
||||||
|
Configuration& configuration,
|
||||||
|
Database& database,
|
||||||
|
Log& log)
|
||||||
{
|
{
|
||||||
// The $TIMEWARRIORDB environment variable overrides the default value of
|
// The $TIMEWARRIORDB environment variable overrides the default value of
|
||||||
// ~/.timewarrior‥
|
// ~/.timewarrior‥
|
||||||
|
@ -120,6 +124,9 @@ void initializeData (Configuration& configuration, Database& database)
|
||||||
// Initialize the database (no data read), but files are enumerated.
|
// Initialize the database (no data read), but files are enumerated.
|
||||||
database.initialize (data._data);
|
database.initialize (data._data);
|
||||||
|
|
||||||
|
// TODO Give the log file a temp fake name. To be removed.
|
||||||
|
log.file (dbLocation._data + "/timewarrior.log");
|
||||||
|
|
||||||
std::cout << "# Configuration\n";
|
std::cout << "# Configuration\n";
|
||||||
for (const auto& name : configuration.all ())
|
for (const auto& name : configuration.all ())
|
||||||
std::cout << "# " << name << "=" << configuration[name] << "\n";
|
std::cout << "# " << name << "=" << configuration[name] << "\n";
|
||||||
|
@ -128,7 +135,10 @@ void initializeData (Configuration& configuration, Database& database)
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void initializeRules (Configuration& configuration, Rules& rules)
|
void initializeRules (
|
||||||
|
Configuration& configuration,
|
||||||
|
Rules& rules,
|
||||||
|
Log& log)
|
||||||
{
|
{
|
||||||
// TODO Load rule grammar.
|
// TODO Load rule grammar.
|
||||||
/*
|
/*
|
||||||
|
@ -148,7 +158,10 @@ void initializeRules (Configuration& configuration, Rules& rules)
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void initializeExtensions (Configuration& configuration, Extensions& extensions)
|
void initializeExtensions (
|
||||||
|
Configuration& configuration,
|
||||||
|
Extensions& extensions,
|
||||||
|
Log& log)
|
||||||
{
|
{
|
||||||
Directory extDir (configuration.get ("db"));
|
Directory extDir (configuration.get ("db"));
|
||||||
extDir += "extensions";
|
extDir += "extensions";
|
||||||
|
@ -165,7 +178,8 @@ int dispatchCommand (
|
||||||
Configuration& configuration,
|
Configuration& configuration,
|
||||||
Database& database,
|
Database& database,
|
||||||
Rules& rules,
|
Rules& rules,
|
||||||
Extensions& extensions)
|
Extensions& extensions,
|
||||||
|
Log& log)
|
||||||
{
|
{
|
||||||
int status {0};
|
int status {0};
|
||||||
|
|
||||||
|
@ -193,7 +207,7 @@ int dispatchCommand (
|
||||||
else if (closeEnough (allCommands[6], argv[1], 2)) status = CmdExport ();
|
else if (closeEnough (allCommands[6], argv[1], 2)) status = CmdExport ();
|
||||||
else if (closeEnough (allCommands[7], argv[1], 2)) status = CmdGaps ();
|
else if (closeEnough (allCommands[7], argv[1], 2)) status = CmdGaps ();
|
||||||
else if (closeEnough (allCommands[8], argv[1], 2)) status = CmdImport ();
|
else if (closeEnough (allCommands[8], argv[1], 2)) status = CmdImport ();
|
||||||
else if (closeEnough (allCommands[9], argv[1], 2)) status = CmdLog ();
|
else if (closeEnough (allCommands[9], argv[1], 2)) status = CmdLog (log);
|
||||||
else if (closeEnough (allCommands[10], argv[1], 2)) status = CmdReport ();
|
else if (closeEnough (allCommands[10], argv[1], 2)) status = CmdReport ();
|
||||||
else if (closeEnough (allCommands[11], argv[1], 2)) status = CmdStart ();
|
else if (closeEnough (allCommands[11], argv[1], 2)) status = CmdStart ();
|
||||||
else if (closeEnough (allCommands[12], argv[1], 2)) status = CmdStop ();
|
else if (closeEnough (allCommands[12], argv[1], 2)) status = CmdStop ();
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <Database.h>
|
#include <Database.h>
|
||||||
#include <Rules.h>
|
#include <Rules.h>
|
||||||
#include <Extensions.h>
|
#include <Extensions.h>
|
||||||
|
#include <Log.h>
|
||||||
//#include <Grammar.h>
|
//#include <Grammar.h>
|
||||||
#include <shared.h>
|
#include <shared.h>
|
||||||
#include <commands.h>
|
#include <commands.h>
|
||||||
|
@ -42,6 +43,11 @@
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int main (int argc, const char** argv)
|
int main (int argc, const char** argv)
|
||||||
{
|
{
|
||||||
|
// The log is needed early, in order to capture as much as possible, but will
|
||||||
|
// only be given a file name once configuration is loaded. The log therefore
|
||||||
|
// buffers the messages until it has a file name to write to.
|
||||||
|
Log log;
|
||||||
|
|
||||||
// Lightweight version checking that doesn't require initialization or I/O.
|
// Lightweight version checking that doesn't require initialization or I/O.
|
||||||
int status = 0;
|
int status = 0;
|
||||||
if (lightweightVersionCheck (argc, argv))
|
if (lightweightVersionCheck (argc, argv))
|
||||||
|
@ -52,7 +58,7 @@ int main (int argc, const char** argv)
|
||||||
// Load the configuration, prepare the database, but do not read data.
|
// Load the configuration, prepare the database, but do not read data.
|
||||||
Configuration configuration;
|
Configuration configuration;
|
||||||
Database database;
|
Database database;
|
||||||
initializeData (configuration, database);
|
initializeData (configuration, database, log);
|
||||||
|
|
||||||
// TODO Arrange the following to minimize memory use.
|
// TODO Arrange the following to minimize memory use.
|
||||||
// TODO Load CLI grammar.
|
// TODO Load CLI grammar.
|
||||||
|
@ -73,31 +79,36 @@ int main (int argc, const char** argv)
|
||||||
|
|
||||||
// Load the rules.
|
// Load the rules.
|
||||||
Rules rules;
|
Rules rules;
|
||||||
initializeRules (configuration, rules);
|
initializeRules (configuration, rules, log);
|
||||||
|
|
||||||
// Load extension script info.
|
// Load extension script info.
|
||||||
Extensions extensions;
|
Extensions extensions;
|
||||||
initializeExtensions (configuration, extensions);
|
initializeExtensions (configuration, extensions, log);
|
||||||
|
|
||||||
// Dispatch to commands.
|
// Dispatch to commands.
|
||||||
status = dispatchCommand (argc, argv, configuration, database, rules, extensions);
|
status = dispatchCommand (argc, argv, configuration, database, rules, extensions, log);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (const std::string& error)
|
catch (const std::string& error)
|
||||||
{
|
{
|
||||||
std::cerr << error << "\n";
|
std::cerr << error << "\n";
|
||||||
|
log.write ("error", error);
|
||||||
status = -1;
|
status = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (std::bad_alloc& error)
|
catch (std::bad_alloc& error)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Memory allocation failed: " << error.what () << "\n";
|
auto message = std::string ("Memory allocation failed: ") + error.what ();
|
||||||
|
std::cerr << "Error: " << message << "\n";
|
||||||
|
log.write ("error", message);
|
||||||
status = -3;
|
status = -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Unknown problem, please report.\n";
|
auto message = "Unknown problem, please report.";
|
||||||
|
std::cerr << "Error: " << message << "\n";
|
||||||
|
log.write ("error", message);
|
||||||
status = -2;
|
status = -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,12 +31,13 @@
|
||||||
#include <Database.h>
|
#include <Database.h>
|
||||||
#include <Rules.h>
|
#include <Rules.h>
|
||||||
#include <Extensions.h>
|
#include <Extensions.h>
|
||||||
|
#include <Log.h>
|
||||||
|
|
||||||
// init.cpp
|
// init.cpp
|
||||||
bool lightweightVersionCheck (int, const char**);
|
bool lightweightVersionCheck (int, const char**);
|
||||||
void initializeData (Configuration&, Database&);
|
void initializeData (Configuration&, Database&, Log&);
|
||||||
void initializeRules (Configuration&, Rules&);
|
void initializeRules (Configuration&, Rules&, Log&);
|
||||||
void initializeExtensions (Configuration&, Extensions&);
|
void initializeExtensions (Configuration&, Extensions&, Log&);
|
||||||
int dispatchCommand (int, const char**, Configuration&, Database&, Rules&, Extensions&);
|
int dispatchCommand (int, const char**, Configuration&, Database&, Rules&, Extensions&, Log&);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue