File: feedback.cc

package info (click to toggle)
mailfilter 0.8.6-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,280 kB
  • ctags: 2,501
  • sloc: cpp: 2,452; sh: 1,393; ansic: 1,259; lex: 570; yacc: 468; makefile: 167; perl: 72
file content (112 lines) | stat: -rw-r--r-- 2,890 bytes parent folder | download | duplicates (5)
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
// feedback.cc - source file for the mailfilter program
// Copyright (c) 2000 - 2009  Andreas Bauer <baueran@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include "feedback.hh"
#include "preferences.hh"

using namespace std;

Feedback* Feedback :: _instance = 0;

Feedback* Feedback :: Instance ()
{
  if (_instance == 0)
    _instance = new Feedback;
  return _instance;
}

Feedback :: ~Feedback ()
{ 
  log_file.close ();
  if (header_file.is_open ())
    header_file.close ();
}

// This function tries to open a user specified log file for write
// access and returns true upon success, false otherwise.

bool Feedback :: open (const char* name)
{
  if (strlen (name))
    {
      log_file.open (name, ios :: app);

      if (!log_file.is_open ())
	return false;

      return true;
    }
    
  return false;
}

// The following two functions attempt to append program messages to
// the end of the log file and returns false, if the file is not
// accessable.  True is returned otherwise.
// 
// min_verbose_level is the minimum value of verbosity that is
// necessary, in order to display a message.  If nothing is
// specified, errors are usually always shown (0).

bool Feedback :: print_msg (const string msg, int min_verbose_level)
{
  if (Preferences :: Instance ().verbose_level () >= min_verbose_level)
    {
      cout << "mailfilter: " << msg << endl;
	
      if (log_file.is_open ())
	log_file << "mailfilter: " << msg << endl;
      else
	return false;
    }

  return true;
}
  
bool Feedback :: print_err (const string msg, int min_verbose_level)
{
  if (Preferences :: Instance ().verbose_level () >= min_verbose_level)
    {
      cerr << "mailfilter: Error: " << msg << endl;
	
      if (log_file.is_open ())
	log_file << "mailfilter: Error: " << msg << endl;
      else
	return false;
    }
    
  return true;
}

bool Feedback :: print_header (const string msg)
{
  if (!header_file.is_open ())
    header_file.open (Preferences :: Instance ().headers_file ().c_str (), ios :: app);
  
  if (header_file.is_open ())
    {
      header_file << msg << endl;
      return true;
    }
  
  return false;
}