Network programming assignment for University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

103 lines
2.5 KiB

  1. /**
  2. * @file Log.java
  3. *
  4. * @author Christos Choutouridis AEM:8997
  5. * @email cchoutou@ece.auth.gr
  6. */
  7. package net.hoo2.auth.vmodem;
  8. /** @name imports */
  9. /** @{ */
  10. import java.io.IOException;
  11. import java.io.PrintWriter;
  12. /** @} */
  13. /**
  14. * @class Log
  15. *
  16. * A common log functionality class for all sessions
  17. */
  18. class Log {
  19. private String logfile_; /**< The log file name */
  20. private boolean verbose_; /**< The desired verbosity (for the console)*/
  21. private PrintWriter writer_; /**< A buffered writer to use for streaming */
  22. /**
  23. * Basic constructor
  24. * @param logfile The log filename
  25. * @param verbose The desired verbosity (for the console)
  26. */
  27. Log (String logfile, boolean verbose) {
  28. logfile_ = logfile;
  29. verbose_ = verbose;
  30. }
  31. /**
  32. * Try to open the log file
  33. * @return The status of the operation
  34. */
  35. boolean open () {
  36. if (logfile_ != null) {
  37. try {
  38. writer_ = new PrintWriter(logfile_);
  39. }
  40. catch (IOException exp) {
  41. System.err.println( "Open log file failed: " + exp.getMessage() );
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. /**
  48. * Try to open a log file
  49. * @param logfile The log file to open
  50. * @return The status of the operation
  51. */
  52. boolean open (String logfile) {
  53. logfile_ = logfile;
  54. return open();
  55. }
  56. /**
  57. * Close the opened file
  58. * @return The status of the operation
  59. */
  60. boolean close () {
  61. try {
  62. if (writer_ != null)
  63. writer_.close();
  64. } catch (Exception ex) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. /**
  70. * Log to file and print to console
  71. * @param line The line to log
  72. * @param out Console output request flag. If true, echo the line to console
  73. */
  74. void write (String line, boolean out) {
  75. if (logfile_ != null) writer_.println(line);
  76. if (verbose_ || out) System.out.println(line);
  77. }
  78. /**
  79. * Log to file and print to console
  80. * @param line The line to log
  81. */
  82. void write (String line) {
  83. if (logfile_ != null) writer_.println(line);
  84. if (verbose_) System.out.println(line);
  85. }
  86. /**
  87. * Echo the line to console
  88. * @param line The line to print
  89. */
  90. void out (String line) {
  91. if (verbose_) System.out.println(line);
  92. }
  93. }