Source Code Cross Referenced for SimpleLog.java in  » ERP-CRM-Financial » Personal-Finance-Manager » br » com » gfp » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » Personal Finance Manager » br.com.gfp.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on 17/10/2005 
003:         * 
004:         * Swing Components - visit http://sf.net/projects/gfd 
005:         * 
006:         * Copyright (C) 2005 Igor Regis da Silva Simões
007:         * 
008:         * This program is free software; you can redistribute it and/or modify it under 
009:         * the terms of the GNU General
010:         * Public License as published by the Free Software Foundation; either version 2 of
011:         * the License, or (at your option) any later version. This program is distributed 
012:         * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
013:         * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
014:         * See the GNU General Public License for more details. You should have received a 
015:         * copy of the GNU General Public License along with this program; if not,
016:         * write to the Free Software Foundation, Inc., 59 Temple Place - 
017:         * Suite 330, Boston, MA 02111-1307, USA.
018:         */
019:        package br.com.gfp.util;
020:
021:        import java.io.File;
022:        import java.io.FileOutputStream;
023:        import java.io.IOException;
024:        import java.io.PrintStream;
025:        import java.text.SimpleDateFormat;
026:        import java.util.Calendar;
027:
028:        /**
029:         * @author Igor Regis da Silva Simoes
030:         * @since 17/10/2005
031:         */
032:        public class SimpleLog {
033:            /**
034:             * Indica se o log está ou não ativado
035:             */
036:            public boolean realizandoLog;
037:
038:            /**
039:             * Diretórios dos arquivos de log
040:             */
041:            private String diretorioArquivoLog;
042:
043:            /**
044:             * Nome do arquivo de log
045:             */
046:            private String arquivoDeLog;
047:
048:            /**
049:             * O io no qual escrevemos o log
050:             */
051:            private PrintStream writer;
052:
053:            /**
054:             * O dia atual do log
055:             */
056:            private int hoje;
057:
058:            /**
059:             * Cria uma instnacia de SimpleLog com os parametros passados
060:             * @param nomeFile Nome padrão do arquivo de log
061:             * @param dir Diretório onde serão escritos os arquivos de log 
062:             */
063:            public SimpleLog(String nomeFile, String dir) {
064:                realizandoLog = false;
065:                setDiretorioLog(dir);
066:                writer = null;
067:                hoje = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
068:                arquivoDeLog = nomeFile;
069:                openTrace();
070:            }
071:
072:            /**
073:             * Fecha os arquivos de log
074:             *
075:             */
076:            public void close() {
077:                realizandoLog = false;
078:                if (writer == null)
079:                    return;
080:                writer.close();
081:            }
082:
083:            /**
084:             * Retorna o nome do arquivo de log
085:             * @return String
086:             */
087:            public String getNomeArquivoLog() {
088:                return arquivoDeLog;
089:            }
090:
091:            /**
092:             * Retorna o nome do diretório de arquivos de log
093:             * @return String
094:             */
095:            public String getDiretorioLog() {
096:                return diretorioArquivoLog;
097:            }
098:
099:            /**
100:             * Indica se o log está ativo, ou seja, se é possível escrever em log
101:             * @return boolean
102:             */
103:            public synchronized boolean isLogAtivo() {
104:                return realizandoLog;
105:            }
106:
107:            /**
108:             * Abre um arquivo de log para ser escrito
109:             * @return boolean indicando se o arquivo foi aberto
110:             */
111:            private boolean openTrace() {
112:                if (realizandoLog)
113:                    return false;
114:                File file = new File(diretorioArquivoLog);
115:                if (!file.exists() && !file.mkdirs())
116:                    return false;
117:
118:                int i = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
119:                File fileDeLog = new File(diretorioArquivoLog + arquivoDeLog
120:                        + i + ".log");
121:                //      O arquivo pode atingir até 2MB
122:                boolean append = fileDeLog.length() <= 1024 * 1024 * 2;
123:
124:                try {
125:                    writer = new PrintStream(new FileOutputStream(fileDeLog,
126:                            append));
127:                } catch (IOException ioexception) {
128:                    return false;
129:                }
130:                //        System.setOut(writer);
131:                //        System.setErr(writer);
132:                realizandoLog = true;
133:                return true;
134:            }
135:
136:            /**
137:             * Muda o diretório dos aquivos de log
138:             * @param String diretório de log
139:             */
140:            private synchronized void setDiretorioLog(String dir) {
141:                if (dir == null)
142:                    return;
143:                if (!dir.endsWith(File.separator))
144:                    dir = dir + File.separator;
145:                File file = new File(dir);
146:                if (!file.exists() && !file.mkdirs())
147:                    dir = ".";
148:                diretorioArquivoLog = dir;
149:            }
150:
151:            /**
152:             * Grava uma mensagem no arquivo de log
153:             * @param mensagem
154:             * @return boolean True Se o log foi gravado com sucesso (sempre retorna false se o log estiver desligado
155:             */
156:            public synchronized boolean log(String mensagem) {
157:                return log(mensagem, null);
158:            }
159:
160:            /**
161:             * Grava uma mensagem no arquivo de log indicando a thread que ocorreu o erro
162:             * @param mensagem 
163:             * @return boolean True Se o log foi gravado com sucesso (sempre retorna false se o log estiver desligado
164:             */
165:            public synchronized boolean log(String mensagem,
166:                    String currentThread) {
167:                if (!realizandoLog)
168:                    return false;
169:                if (mensagem == null || writer == null)
170:                    return false;
171:                if (currentThread == null)
172:                    currentThread = Thread.currentThread().getName();
173:
174:                SimpleDateFormat simpledateformat = new SimpleDateFormat(
175:                        "dd-MM-yyyy-HH:mm:ss:SSS");
176:                try {
177:                    Calendar calendar = Calendar.getInstance();
178:                    String mensagemFinal;
179:                    mensagemFinal = simpledateformat.format(calendar.getTime())
180:                            + " " + currentThread + ":" + mensagem;
181:
182:                    if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) != hoje) {
183:                        writer
184:                                .println("**********************************************");
185:                        writer.println("LOG - Encerrando log do dia " + hoje);
186:                        writer
187:                                .println("**********************************************");
188:                        writer.flush();
189:                        close();
190:                        hoje = Calendar.getInstance()
191:                                .get(Calendar.DAY_OF_MONTH);
192:                        openTrace();
193:                        writer
194:                                .println("**********************************************");
195:                        writer.println("LOG - Abrindo log do dia " + hoje);
196:                        writer
197:                                .println("**********************************************");
198:                        writer.flush();
199:                    }
200:                    writer.println(mensagemFinal);
201:                    writer.flush();
202:                } catch (Exception exception) {
203:                    return false;
204:                }
205:                return true;
206:            }
207:
208:            /**
209:             * Imprime a pilha de erro do parametro
210:             * @param throwable
211:             * @return boolean True Se o log foi gravado com sucesso (sempre retorna false se o log estiver desligado
212:             */
213:            public boolean log(Throwable throwable) {
214:                return log(throwable, null);
215:            }
216:
217:            /**
218:             * Imprime a pilha de erro do parametro indicando a thread que ocorreu o erro
219:             * @param throwable
220:             * @return boolean True Se o log foi gravado com sucesso (sempre retorna false se o log estiver desligado
221:             */
222:            public synchronized boolean log(Throwable throwable,
223:                    String currentThread) {
224:                SimpleDateFormat simpledateformat = new SimpleDateFormat(
225:                        "dd-MM-yyyy-HH:mm:ss:SSS");
226:                if (!realizandoLog)
227:                    return false;
228:                if (currentThread == null)
229:                    currentThread = Thread.currentThread().getName();
230:                try {
231:                    Calendar calendar = Calendar.getInstance();
232:                    String s = simpledateformat.format(calendar.getTime())
233:                            + " " + currentThread + ":";
234:                    writer.print(s);
235:                    throwable.printStackTrace(writer);
236:                    writer.flush();
237:                } catch (Exception exception) {
238:                    return false;
239:                }
240:                return true;
241:            }
242:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.