cli_output.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009 Sean Nelson <audiohacked@gmail.com>
  5. * Copyright (C) 2011 Carl-Daniel Hailfinger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include "flash.h"
  26. #ifndef STANDALONE
  27. static FILE *logfile = NULL;
  28. int close_logfile(void)
  29. {
  30. if (!logfile)
  31. return 0;
  32. /* No need to call fflush() explicitly, fclose() already does that. */
  33. if (fclose(logfile)) {
  34. /* fclose returned an error. Stop writing to be safe. */
  35. logfile = NULL;
  36. msg_perr("Closing the log file returned error %s\n", strerror(errno));
  37. return 1;
  38. }
  39. logfile = NULL;
  40. return 0;
  41. }
  42. int open_logfile(const char * const filename)
  43. {
  44. if (!filename) {
  45. msg_gerr("No filename specified.\n");
  46. return 1;
  47. }
  48. if ((logfile = fopen(filename, "w")) == NULL) {
  49. perror(filename);
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. void start_logging(void)
  55. {
  56. int oldverbose_screen = verbose_screen;
  57. /* make the console quieter. */
  58. verbose_screen = MSG_ERROR;
  59. print_version();
  60. verbose_screen = oldverbose_screen;
  61. }
  62. #endif /* !STANDALONE */
  63. int print(int type, const char *fmt, ...)
  64. {
  65. va_list ap;
  66. int ret;
  67. FILE *output_type;
  68. switch (type) {
  69. case MSG_ERROR:
  70. output_type = stderr;
  71. break;
  72. case MSG_BARF:
  73. if (verbose_screen < 3)
  74. return 0;
  75. case MSG_DEBUG2:
  76. if (verbose_screen < 2)
  77. return 0;
  78. case MSG_DEBUG:
  79. if (verbose_screen < 1)
  80. return 0;
  81. case MSG_INFO:
  82. default:
  83. output_type = stdout;
  84. break;
  85. }
  86. va_start(ap, fmt);
  87. ret = vfprintf(output_type, fmt, ap);
  88. va_end(ap);
  89. fflush(output_type);
  90. #ifndef STANDALONE
  91. if ((type <= verbose_logfile) && logfile) {
  92. va_start(ap, fmt);
  93. ret = vfprintf(logfile, fmt, ap);
  94. va_end(ap);
  95. if (type != MSG_BARF)
  96. fflush(logfile);
  97. }
  98. #endif /* !STANDALONE */
  99. return ret;
  100. }