summaryrefslogtreecommitdiff
path: root/netsvcs/lib/Logging_Strategy.cpp
blob: a0cddc361766a3c82c31e1f8220416e00c8c6dcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Logging_Strategy.cpp
// $Id$

#define ACE_BUILD_SVC_DLL
#include "iostream.h"
#include "fstream.h"
#include "ace/Get_Opt.h"

#include "ace/Service_Object.h"
#include "Logging_Strategy.h"

class ACE_Logging_Strategy : public ACE_Service_Object
  // = TITLE
  //     This class provides the hooks to control the output produced
  //     by any of the network services. 
  // 
  // = DESCRIPTION
  //   	 Depending upon when this service is invoked and with what
  //     flags, the output of other network services can be
  //     controlled. The output can be streamed to stderr, to a file,
  //     to a logging daemon, or it can be set to be "silent".
{
public:
  virtual int init (int argc, char *argv[]);
  // Dynamic linking hook.

  int parse_args (int argc, char *argv[]);
  // Parse svc.conf arguments.
private:
  void tokenize (char *flag_string);
  // Tokenize to set all the flags
  u_long flags_;
  char *filename_;
};

// Parse the string containing all the flags and set the flags accordingly
void
ACE_Logging_Strategy::tokenize (char *flag_string)
{
  char *flag;
  if ((flag = ACE_OS::strtok (flag_string, "|")) != NULL)
    {
      while (flag)
	{
	  if (ACE_OS::strcmp (flag, "STDERR") == 0)
	    ACE_SET_BITS (this->flags_, ACE_Log_Msg::STDERR);
	  else if (ACE_OS::strcmp (flag, "LOGGER") == 0)
	    ACE_SET_BITS (this->flags_, ACE_Log_Msg::LOGGER);
	  else if (ACE_OS::strcmp (flag, "OSTREAM") == 0)
	    ACE_SET_BITS (this->flags_, ACE_Log_Msg::OSTREAM);
	  else if (ACE_OS::strcmp (flag, "VERBOSE") == 0)
	    ACE_SET_BITS (this->flags_, ACE_Log_Msg::VERBOSE);
	  else if (ACE_OS::strcmp (flag, "SILENT") == 0)
	    ACE_SET_BITS (this->flags_, ACE_Log_Msg::SILENT);

	  // Get the next flag
	  flag = ACE_OS::strtok(0, "|");
	}
    }
}

int
ACE_Logging_Strategy::parse_args (int argc, char *argv[])
{
  ACE_TRACE ("ACE_Logging_Strategy::parse_args");
  char *temp;

  this->flags_ = 0;
  this->filename_ = ACE_DEFAULT_LOGFILE;

  ACE_LOG_MSG->open ("Logging_Strategy");

  ACE_Get_Opt get_opt (argc, argv, "f:s:", 0);

  for (int c; (c = get_opt ()) != -1; )
    {
      switch (c)
	{
	case 'f':
	  temp = get_opt.optarg;
	  // Now tokenize the string to get all the flags
	  this->tokenize (temp);
	  break;
	case 's':
	  // Ensure that the OSTREAM flag is set
	  ACE_SET_BITS (this->flags_, ACE_Log_Msg::OSTREAM);
	  this->filename_ = get_opt.optarg;
	  break;
	default:
	  break;
	}
    }
  return 0;
}

int
ACE_Logging_Strategy::init (int argc, char *argv[])
{
  ACE_TRACE ("ACE_Logging_Strategy::init");

  // Use the options hook to parse the command line arguments.
  this->parse_args (argc, argv);

  // Check if any flags were specified. If none were specified, let
  // the default behavior take effect.
  if (this->flags_ != 0)
    {
      // Clear all flags
      ACE_Log_Msg::instance()->clr_flags (ACE_Log_Msg::STDERR  | 
					  ACE_Log_Msg::LOGGER  |
					  ACE_Log_Msg::OSTREAM |
					  ACE_Log_Msg::VERBOSE |
					  ACE_Log_Msg::SILENT);
      // Check if OSTREAM bit is set
      if (ACE_BIT_ENABLED (this->flags_, ACE_Log_Msg::OSTREAM))
	{
	  // Create a new ofstream to direct output to the file
	  ofstream *output_file = new ofstream (this->filename_);
	  ACE_Log_Msg::instance()->msg_ostream (output_file);
	}
      // Now set the flags for Log_Msg
      ACE_Log_Msg::instance()->set_flags (this->flags_);
    }
  return 0;
}

// The following is a "Factory" used by the ACE_Service_Config and
// svc.conf file to dynamically initialize the state of the Logging_Strategy.

ACE_SVC_FACTORY_DEFINE (ACE_Logging_Strategy)