001    package ca.uhn.hl7v2.util;
002    
003    import java.io.*;
004    import java.util.GregorianCalendar;
005    import java.util.Date;
006    
007    /**
008     * <p>Handles output of status messages.  Authors are encouraged to provide status 
009     * information using the methods: </p>
010     * <p><code>writeStatus(String message);<br>
011     * writeVerboseStatus(String message);</code></p>
012     * <p>... instead of using <code>System.out.println(...)</code>.  This allows the user
013     * to control the output of status messages in one place. </p>
014     * <p>By default, messages are written to std out, and only "standard" messages are 
015     * written (i.e. from calls to <code>writeStatus()</code>, not <code>writeVerboseStatus()</code>).
016     * This can be changed at startup by setting the following system properties: </p>
017     * <p>ca.uhn.hl7v2.util.status.out = ["STANDARD_OUT" | "FILE"]<br>
018     * ca.uhn.hl7v2.util.status.level = ["NONE" | "STANDARD" | "VERBOSE"]</p>
019     * <p>If a programmer wishes to log status messages more reliably and without user control, (s)he
020     * should use <code>ca.uhn.hl7v2.Log</code> instead.</p>
021     *
022     * @deprecated use ca.uhn.log package
023     * @author Bryan Tripp
024     * 
025     */
026    public class Status {
027        
028        private int dest = 0;
029        private int detail = 0;
030        private BufferedWriter fileOut;
031        
032        private static Status singleton = null;
033    
034        /**
035         * Used as an argument to setOutput(), causes status information to be printed 
036         * to standard out.
037         */
038        public static final int STANDARD_OUT = 1;
039        
040        /**
041         * Used as an argument to setOutput(), causes status information to be printed 
042         * to the file "status.log" in the working directory. 
043         */
044        public static final int FILE = 2;
045        
046        /**
047         * Used as an argument to setVerbosity(), causes no status information to 
048         * be output.  
049         */
050        public static final int NONE = 3;
051        
052        /**
053         * Used as an argument to setVerbosity(), causes standard status information to 
054         * be output.  
055         */
056        public static final int STANDARD = 4;
057        
058        /**
059         * Used as an argument to setVerbosity(), causes detailed status information to 
060         * be output.  
061         */
062        public static final int VERBOSE = 5;
063        
064        /** 
065         * Creates a new instance of Status 
066         */
067        private Status() {
068            //check system properties for configuration info
069            String out = System.getProperty("ca.uhn.hl7v2.util.status.out");
070            String level = System.getProperty("ca.uhn.hl7v2.util.status.level");
071            
072            if (out != null && out.equals("FILE")) {
073                setOutput(Status.FILE);
074            } else {
075                setOutput(Status.STANDARD_OUT);
076            }
077            
078            if (level != null && level.equals("NONE")) {
079                setVerbosity(Status.NONE);
080            } else if (level != null && level.equals("VERBOSE")) {
081                setVerbosity(Status.VERBOSE);
082            } else {
083                setVerbosity(Status.STANDARD);
084            }
085        }
086        
087        /** 
088         * Sets the location where status messages are output.  Valid values include 
089         * Status.STANDARD_OUT (std out) and Status.FILE (messages output to <hapi.home>/status.log)
090         */
091        public synchronized void setOutput(int destination) {
092            if (destination == Status.STANDARD_OUT || destination == Status.FILE) {
093                this.dest = destination;
094                if (dest == Status.STANDARD_OUT) {
095                    System.out.println("Outputting status messages to standard out.");
096                } else {
097                    String file = Home.getHomeDirectory().getAbsolutePath() + "/status.log";
098                    System.out.println("Outputting status messages to the file " + file);
099                    try {
100                        this.fileOut = new BufferedWriter(new FileWriter(file, true));
101                    } catch (IOException e) {
102                        System.err.println("Couldn't open file status.log for status output - using std out instead.");
103                        this.dest = Status.STANDARD_OUT;
104                    }
105                }
106            }
107        }
108        
109        /**
110         * Sets the level of detail of status messages.  Valid values include 
111         * Status.NONE, Status.STANDARD, and Status.VERBOSE (detailed).
112         */
113        public synchronized void setVerbosity(int level) {
114            if (level == Status.NONE || level == Status.STANDARD || level == Status.VERBOSE) {
115                this.detail = level;
116                if (detail == Status.NONE) {
117                    System.out.println("ca.uhn.hl7v2.util.Status: No status messages will be output.");
118                } else if (detail == Status.STANDARD) {
119                    System.out.println("ca.uhn.hl7v2.util.Status: Standard status messages will be output.");
120                } else { 
121                    System.out.println("ca.uhn.hl7v2.util.Status: Verbose status messages will be output.");
122                }
123            }
124        }
125    
126        /** 
127         * Outputs a standard status message as per configuration. 
128         */
129        public static void writeStatus(String message) {
130            if (singleton == null) {
131                //initialize instance
132                singleton = new Status();
133            }
134         
135            singleton.writeStatus(message, false);
136        }
137        
138        /** 
139         * Outputs a detail status message as per configuration. 
140         */
141        public static void writeVerboseStatus(String message) {
142            if (singleton == null) {
143                //initialize instance
144                singleton = new Status();
145            }
146         
147            singleton.writeStatus(message, true);        
148        }
149        
150        /**
151         * Should be called only through static method of same name.
152         */
153        private synchronized void writeStatus(String message, boolean verbose) {
154            boolean output = true;
155            if (this.detail == Status.NONE || (verbose && this.detail != Status.VERBOSE)) output = false;
156            
157            GregorianCalendar now = new GregorianCalendar();
158            now.setTime(new Date());
159            StringBuffer mess = new StringBuffer();
160            mess.append(now.get(GregorianCalendar.DAY_OF_MONTH));
161            mess.append("/");
162            mess.append(now.get(GregorianCalendar.MONTH + 1));
163            mess.append("/");
164            mess.append(now.get(GregorianCalendar.YEAR));
165            mess.append(" ");
166            mess.append(now.get(GregorianCalendar.HOUR_OF_DAY));
167            mess.append(":");
168            mess.append(now.get(GregorianCalendar.MINUTE));
169            mess.append(":");
170            mess.append(now.get(GregorianCalendar.SECOND));
171            mess.append(".");
172            mess.append(now.get(GregorianCalendar.MILLISECOND));
173            mess.append("  ");
174            mess.append(message);
175            
176            if (output) {
177                if (this.dest == Status.STANDARD_OUT) {
178                    System.out.println(mess.toString());
179                } else {
180                    try {
181                        this.fileOut.write(mess.toString());
182                        this.fileOut.write("\r\n");
183                        this.fileOut.flush();
184                    } catch (IOException e) {
185                        System.err.println("Error writing status message to file: " + e.getMessage());
186                        System.out.println(mess.toString());
187                    }
188                }
189            }
190        }
191    }