Merge branch 'release-2.140.101'
[feisty_meow.git] / nucleus / library / application / application_shell.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : application_shell                                                 *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "application_shell.h"
16
17 #include <basis/astring.h>
18 #include <basis/functions.h>
19 #include <configuration/application_configuration.h>
20 #include <loggers/combo_logger.h>
21 #include <loggers/program_wide_logger.h>
22 #include <timely/time_stamp.h>
23
24 #include <stdio.h>
25 #ifdef __UNIX__
26   #include <limits.h>
27   #include <unistd.h>
28 #endif
29
30 #undef LOG
31 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
32
33 using namespace basis;
34 using namespace configuration;
35 using namespace loggers;
36 using namespace mathematics;
37 using namespace textual;
38 using namespace timely;
39
40 namespace application {
41
42 const int MAXIMUM_COMMAND_LINE = 32 * KILOBYTE;
43   // maximum command line that we'll deal with here.
44
45 application_shell *not_so_hidden_pointer = NULL;  // fodder for the single instance function.
46
47 application_shell *application_shell::single_instance() { return not_so_hidden_pointer; }
48
49 application_shell::application_shell()
50 : c_rando(),
51   c_exit_value(120)  // if this is never changed from the default, that in itself is an error.
52 {
53   // we create a combo logger for program-wide usage.
54   SETUP_COMBO_LOGGER;
55   not_so_hidden_pointer = this;  // setup the single instance method.
56 }
57
58 application_shell::~application_shell()
59 {
60   if (not_so_hidden_pointer == this) not_so_hidden_pointer = NULL;  // only scorch it when it's us.
61 }
62
63 outcome application_shell::log(const base_string &to_print, int filter)
64 {
65   outcome to_return = common::OKAY;
66   if (program_wide_logger::get().member(filter)) {
67     astring temp_log(to_print);
68     if (temp_log.length())
69       temp_log.insert(0, time_stamp::notarize(true));
70     to_return = program_wide_logger::get().log(temp_log, filter);
71   }
72   return to_return;
73 }
74
75 int application_shell::execute_application()
76 {
77   try {
78     c_exit_value = execute();
79   } catch (const char *message) {
80     printf("caught exception:\n%s\n", message);
81   } catch (astring &message) {
82     printf("caught exception:\n%s\n", message.s());
83   } catch (...) {
84     printf("caught exception: unknown type!\n");
85   }
86   return c_exit_value;
87 }
88
89 } //namespace.
90