feisty meow concerns codebase  2.140
application_shell.cpp
Go to the documentation of this file.
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>
20 #include <loggers/combo_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.
55  not_so_hidden_pointer = this; // setup the single instance method.
56 }
57 
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 
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 
The application_shell is a base object for console programs.
virtual basis::outcome log(const basis::base_string &to_print, int filter=basis::ALWAYS_PRINT)
< returns the full name of the current application.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
virtual int execute_application()
runs the base class's execute() method and catches any exceptions due to it.
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
void insert(int position, const astring &to_insert)
Copies "to_insert" into "this" at the "position".
Definition: astring.cpp:892
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
Defines the base class for all string processing objects in hoople.
Definition: base_string.h:28
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
#define SETUP_COMBO_LOGGER
a macro that retasks the program-wide logger as a combo_logger.
Definition: combo_logger.h:49
Implements an application lock to ensure only one is running at once.
application_shell * not_so_hidden_pointer
const int MAXIMUM_COMMAND_LINE
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
const int KILOBYTE
Number of bytes in a kilobyte.
Definition: definitions.h:134
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
#include <time.h>
Definition: earth_time.cpp:37