cli_output.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. int verbose_screen = MSG_INFO;
  27. int verbose_logfile = MSG_DEBUG2;
  28. #ifndef STANDALONE
  29. static FILE *logfile = NULL;
  30. int close_logfile(void)
  31. {
  32. if (!logfile)
  33. return 0;
  34. /* No need to call fflush() explicitly, fclose() already does that. */
  35. if (fclose(logfile)) {
  36. /* fclose returned an error. Stop writing to be safe. */
  37. logfile = NULL;
  38. msg_gerr("Closing the log file returned error %s\n", strerror(errno));
  39. return 1;
  40. }
  41. logfile = NULL;
  42. return 0;
  43. }
  44. int open_logfile(const char * const filename)
  45. {
  46. if (!filename) {
  47. msg_gerr("No logfile name specified.\n");
  48. return 1;
  49. }
  50. if ((logfile = fopen(filename, "w")) == NULL) {
  51. msg_gerr("Error: opening log file \"%s\" failed: %s\n", filename, strerror(errno));
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. void start_logging(void)
  57. {
  58. enum msglevel oldverbose_screen = verbose_screen;
  59. /* Shut up the console. */
  60. verbose_screen = MSG_ERROR;
  61. print_version();
  62. verbose_screen = oldverbose_screen;
  63. }
  64. #endif /* !STANDALONE */
  65. /* Please note that level is the verbosity, not the importance of the message. */
  66. int print(enum msglevel level, const char *fmt, ...)
  67. {
  68. va_list ap;
  69. int ret = 0;
  70. FILE *output_type = stdout;
  71. if (level < MSG_INFO)
  72. output_type = stderr;
  73. if (level <= verbose_screen) {
  74. va_start(ap, fmt);
  75. ret = vfprintf(output_type, fmt, ap);
  76. va_end(ap);
  77. /* msg_*spew often happens inside chip accessors in possibly
  78. * time-critical operations. Don't slow them down by flushing. */
  79. if (level != MSG_SPEW)
  80. fflush(output_type);
  81. }
  82. #ifndef STANDALONE
  83. /* skip of msgs starting from '\b' added to skip progress percents */
  84. if ((level <= verbose_logfile) && logfile && (!fmt || fmt[0] != '\b')) {
  85. va_start(ap, fmt);
  86. ret = vfprintf(logfile, fmt, ap);
  87. va_end(ap);
  88. if (level != MSG_SPEW)
  89. fflush(logfile);
  90. }
  91. #endif /* !STANDALONE */
  92. return ret;
  93. }