PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/runtime/ext/ext_output.cpp

https://github.com/zsj888/hiphop-php
C++ | 71 lines | 46 code | 7 blank | 18 comment | 15 complexity | 57630bb22fb8046cd111b31ceb818778 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010 Facebook, Inc. (http://www.facebook.com) |
  6. | Copyright (c) 1997-2010 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include <runtime/ext/ext_output.h>
  18. #include <runtime/base/runtime_option.h>
  19. #include <util/lock.h>
  20. namespace HPHP {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. static ReadWriteMutex s_loggers_mutex;
  23. typedef std::map<std::string, FILE*> LoggerMap;
  24. static LoggerMap s_loggers;
  25. bool f_hphp_log(CStrRef filename, CStrRef message) {
  26. if (!RuntimeOption::EnableApplicationLog) {
  27. return false;
  28. }
  29. FILE *f = NULL;
  30. {
  31. ReadLock lock(s_loggers_mutex);
  32. LoggerMap::const_iterator iter = s_loggers.find(filename.data());
  33. if (iter != s_loggers.end()) {
  34. f = iter->second;
  35. }
  36. }
  37. if (f == NULL) {
  38. WriteLock lock(s_loggers_mutex);
  39. LoggerMap::const_iterator iter = s_loggers.find(filename.data());
  40. if (iter != s_loggers.end()) {
  41. f = iter->second;
  42. } else {
  43. if (filename.charAt(0) == '|') {
  44. f = popen(filename.data() + 1, "w");
  45. } else {
  46. f = fopen(filename.data(), "a");
  47. }
  48. if (f == NULL) {
  49. return false;
  50. }
  51. s_loggers[filename.data()] = f;
  52. }
  53. }
  54. bool ret = (fwrite(message.data(), message.size(), 1, f) == 1);
  55. if (ret) {
  56. fflush(f);
  57. }
  58. return ret;
  59. }
  60. void f_hphp_crash_log(CStrRef name, CStrRef value) {
  61. StackTraceNoHeap::AddExtraLogging(name.data(), value.data());
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. }