envutil.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief Environment utilities implementation.
  19. */
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include "util/envutil.h"
  23. static char const* prog_name = NULL;
  24. void set_prog_name(char const* name) { prog_name = name; }
  25. char const* get_prog_name() { return prog_name; }
  26. int log_error(char const* msg, ...) {
  27. int result = 0;
  28. int local_result = 0;
  29. va_list args;
  30. va_start(args, msg);
  31. do {
  32. local_result = fprintf(stderr, "%s: ", prog_name);
  33. if (local_result < 0) {
  34. result = local_result;
  35. break;
  36. }
  37. result += local_result;
  38. local_result = vfprintf(stderr, msg, args);
  39. if (local_result < 0) {
  40. result = local_result;
  41. break;
  42. }
  43. result += local_result;
  44. local_result = fprintf(stderr, "\n");
  45. if (local_result < 0) {
  46. result = local_result;
  47. break;
  48. }
  49. result += local_result;
  50. } while (0);
  51. va_end(args);
  52. return result;
  53. }
  54. int log_msg(char const* msg, ...) {
  55. int result = 0;
  56. int local_result = 0;
  57. va_list args;
  58. va_start(args, msg);
  59. do {
  60. local_result = vfprintf(stdout, msg, args);
  61. if (local_result < 0) {
  62. result = local_result;
  63. break;
  64. }
  65. result += local_result;
  66. local_result = fprintf(stdout, "\n");
  67. if (local_result < 0) {
  68. result = local_result;
  69. break;
  70. }
  71. result += local_result;
  72. } while (0);
  73. va_end(args);
  74. return result;
  75. }
  76. int log_fmt(char const* msg, ...) {
  77. int result = 0;
  78. va_list args;
  79. va_start(args, msg);
  80. result = vfprintf(stdout, msg, args);
  81. va_end(args);
  82. return result;
  83. }