Remove dynamic exceptions
[anna.git] / example / core / trace / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <iostream>
10
11 #include <functional>
12
13 #include <anna/core/core.hpp>
14
15 #include <anna/app/Application.hpp>
16
17 class Test : public app::Application {
18 public:
19    Test ();
20    
21    void initialize () noexcept(false);
22    
23 private:
24    void  run () noexcept(false);      
25 };
26
27 using namespace std;
28
29 int main (int argc, const char** argv)
30 {
31    CommandLine& commandLine (CommandLine::instantiate ());
32    Test test;
33
34    try {
35       commandLine.initialize (argv, argc);
36       commandLine.verify ();
37
38       if (commandLine.exists ("t"))
39          Logger::setLevel (Logger::asLevel (commandLine.getValue ("t"))); 
40
41       Logger::initialize ("testfunctions", new TraceWriter ("file.trace", 2048000));
42  
43       test.start ();
44    }
45    catch (Exception& ex) {
46       cout << ex.asString () << endl;
47    }
48    
49    return 0;
50 }
51
52 Test::Test () : 
53    app::Application ("testfunctions", "Checking trace system", "1.0") 
54 {
55    CommandLine& commandLine (CommandLine::instantiate ());
56       
57    commandLine.add ("s", CommandLine::Argument::Mandatory, "String");
58    commandLine.add ("n", CommandLine::Argument::Mandatory, "Number of loops");
59    commandLine.add ("t", CommandLine::Argument::Optional, "Initial trace level");
60 }
61
62 void Test::initialize () 
63    noexcept(false)
64 {
65    CommandLine& commandLine (CommandLine::instantiate ());
66
67    if (commandLine.exists ("t"))
68       Logger::setLevel (Logger::asLevel (commandLine.getValue ("t"))); 
69 }
70
71 void Test::run () 
72    noexcept(false)
73 {
74    CommandLine& commandLine (CommandLine::instantiate ());
75
76    cout << "Second: " << functions::second () << endl;
77    cout << "MilliSecond: " << functions::millisecond () << endl << endl;
78
79    string str (commandLine.getValue ("s"));
80    int n (commandLine.getIntegerValue ("n"));
81    
82    Logger::Level level = Logger::getLevel ();
83
84    for (int i = 0; i < n; i ++) {
85       if ((i % 2) == 0)
86          Logger::write (level, "string", str.c_str (), ANNA_FILE_LOCATION);
87       else
88          Logger::write (level, "level", (int) level, ANNA_FILE_LOCATION);
89          
90       level = Logger::Level ((int) level + 1);
91       
92       if (level >= Logger::Local0)
93          level = Logger::Emergency;
94       else if (level > Logger::Debug)
95          level = Logger::Local0;
96    }
97 }
98