-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogServer.java
More file actions
136 lines (119 loc) · 4.13 KB
/
LogServer.java
File metadata and controls
136 lines (119 loc) · 4.13 KB
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
131
132
133
134
135
136
package com.legrand.iln;
import java.io.*;
import java.util.*;
/* $Id: LogServer.java,v 1.4 2003/06/02 20:41:09 legrand Exp legrand $ */
/**
* Progetto ILN
* Copyright (C) 2003 Monsieur Legrand
*
* 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 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
*/
/**
* Classe che permette a qualunque server di rete che estende
* <code>GenericServer</code> di loggare messaggi su file system
* locale. Serve per motivi di controllo e di sicurezza.
*
* @author Monsieur Legrand
* @version 1.4 rev 0
* @date 02 giugno 2003
*/
public class LogServer
{
private String nomeServizio;
private PrintWriter out;
private FileWriter outStream;
/**
* Costruttore. Istanzia un nuovo log associato ad un file su File System.
*
* @param name Il nome del file di log. Ad esso verra' aggiunta automaticamente l'estensione
* .log
*/
public LogServer(String name){
nomeServizio = name;
Date today = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(today);
String timeStart = setStringona(cal);
try{
outStream = new FileWriter(nomeServizio + ".log", true);
out = new PrintWriter(outStream, true);
} catch(IOException ec){
System.out.println("Errore nell'accesso al file di log: " + nomeServizio + ".log");
ec.printStackTrace();
}
out.println("");
out.println("****** Inizio logging " + nomeServizio + " " + timeStart + " ******");
}
/**
* Scrive su file di log un messaggio.<BR>
* E' responsabilita' del chiamante il montaggio della stringa messaggio.<BR>
* Questo metodo provvedera' solo ad agganciare davanti al messaggio il nome
* del servizio ed il TimeStamp.
*
* @param req Il messaggio da loggare.
*/
public void logga(String req){
Date today = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(today);
String timeNow = setStringhina(cal);
System.out.println(timeNow + " " + req);
out.println(timeNow + " " + req);
}
/**
* Chiude il file di log.
*/
public void close(){
try{
out.close();
outStream.close();
}catch(IOException ex){
System.out.println("Errore durante la chiusura del file di log");
ex.printStackTrace();
}
}
private String setStringona(GregorianCalendar cal){
String am_pm[] = {"am", "pm"};
String risposta = "" + cal.get(Calendar.DATE);
risposta += "/" + (cal.get(Calendar.MONTH) + 1);
risposta += "/" + cal.get(Calendar.YEAR);
if (am_pm[cal.get(Calendar.AM_PM)].equals("am"))
risposta += " " + cal.get(Calendar.HOUR);
else risposta += " " + (cal.get(Calendar.HOUR) + 12);
if (cal.get(Calendar.MINUTE) < 10)
risposta += ":0" + cal.get(Calendar.MINUTE);
else
risposta += ":" + cal.get(Calendar.MINUTE);
if (cal.get(Calendar.SECOND) < 10)
risposta += ":0" + cal.get(Calendar.SECOND);
else
risposta += ":" + cal.get(Calendar.SECOND);
return risposta;
}
private String setStringhina(GregorianCalendar cal){
String am_pm[] = {"am", "pm"};
String risposta;
if (am_pm[cal.get(Calendar.AM_PM)].equals("am"))
risposta = "" + cal.get(Calendar.HOUR);
else risposta = "" + (cal.get(Calendar.HOUR) + 12);
if (cal.get(Calendar.MINUTE) < 10)
risposta += ":0" + cal.get(Calendar.MINUTE);
else risposta += ":" + cal.get(Calendar.MINUTE);
if (cal.get(Calendar.SECOND) < 10)
risposta += ":0" + cal.get(Calendar.SECOND);
else risposta += ":" + cal.get(Calendar.SECOND);
return risposta;
}
}