Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #define ANSI_COLOR_RED "\x1b[31m"
  4. #define ANSI_COLOR_GREEN "\x1b[32m"
  5. #define ANSI_COLOR_YELLOW "\x1b[33m"
  6. #define ANSI_COLOR_BLUE "\x1b[34m"
  7. #define ANSI_COLOR_MAGENTA "\x1b[35m"
  8. #define ANSI_COLOR_CYAN "\x1b[36m"
  9. #define ANSI_COLOR_RESET "\x1b[m"
  10.  
  11. using namespace std;
  12.  
  13. class FileOutputStream
  14. {
  15. public:
  16.  
  17. FileOutputStream(char* filename): file_(fopen(filename, "w"))
  18. {}
  19.  
  20. void print(int t)
  21. {
  22. fprintf(file_, "%d", t);
  23. }
  24.  
  25. void print(double t)
  26. {
  27. fprintf(file_, "%f", t);
  28. }
  29.  
  30. void print(char* name)
  31. {
  32. fprintf(file_, "%s", name);
  33. }
  34.  
  35. void flush()
  36. {
  37. fflush(file_);
  38. }
  39.  
  40. ~FileOutputStream()
  41. {
  42. fclose(file_);
  43. }
  44.  
  45. public:
  46. FileOutputStream(FILE* file): file_(file)
  47. {}
  48.  
  49.  
  50. private:
  51. FileOutputStream& operator = (FileOutputStream const&);
  52. FileOutputStream(FileOutputStream const&);
  53.  
  54. private:
  55. FILE* file_;
  56. };
  57.  
  58.  
  59. class ConsoleOutputStream : public FileOutputStream
  60. {
  61. public:
  62. ConsoleOutputStream() : FileOutputStream(stdout)
  63. {}
  64. void setColor(const char* str)
  65. {
  66. printf(str);
  67. }
  68. ~ConsoleOutputStream() {}
  69. };
  70.  
  71. class ErrorOutputStream : FileOutputStream
  72. {
  73. public:
  74.  
  75. ErrorOutputStream() : FileOutputStream(stderr) {}
  76.  
  77. void print(const char* name, const char* file, const char* func, int line)
  78. {
  79. cout << name << ' ' << file << ' ' << func << ' ' << line;
  80. }
  81.  
  82. ~ErrorOutputStream() { }
  83. };
  84.  
  85.  
  86. int main (int argc, char const *argv[])
  87. {
  88.  
  89. ConsoleOutputStream a;
  90. ErrorOutputStream s;
  91. a.setColor(ANSI_COLOR_RED);
  92. s.print("Error!!", __FILE__, __FUNCTION__, __LINE__);
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement