PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Debug/docs/examples/trigger_error.php

https://github.com/Yannix/zetacomponents
PHP | 128 lines | 52 code | 24 blank | 52 comment | 8 complexity | 8fa929b18309a7528fd325f809115eb2 MD5 | raw file
  1. <?php
  2. /**
  3. * This example demonstrates how the Debugger and Log can be used in
  4. * combination with the trigger_error.
  5. *
  6. * Licensed to the Apache Software Foundation (ASF) under one
  7. * or more contributor license agreements. See the NOTICE file
  8. * distributed with this work for additional information
  9. * regarding copyright ownership. The ASF licenses this file
  10. * to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance
  12. * with the License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing,
  17. * software distributed under the License is distributed on an
  18. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19. * KIND, either express or implied. See the License for the
  20. * specific language governing permissions and limitations
  21. * under the License.
  22. *
  23. * @package Debug
  24. * @version //autogentag//
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Callback function for the trigger_error function.
  29. */
  30. function errorHandler( $code, $msg, $file, $line )
  31. {
  32. if ( /*debug = */ true && $code == E_USER_NOTICE )
  33. {
  34. // ezcDebug is auto_included, therefore when debug is disabled this class is not loaded at all.
  35. // Func: START_TIMER, STOP_TIMER, or LOG.
  36. // Msg: Message without the 'encoded parameters'.
  37. // source: Paynet, example, etc.
  38. // category: Template, SQL, etc.
  39. $dm = new ezcDebugMessage( $msg );
  40. $dm->setDefaultSource( "Paynet" );
  41. if ( $dm->isLog())
  42. {
  43. ezcDebug::getInstance()->log( $dm->message, $dm->verbosity, $dm->source,
  44. $dm->category, array( "file" => $file, "line" => $line ) );
  45. }
  46. else if ( $parsedMsg->isStartTimer() )
  47. {
  48. $ezcDebug->getInstance()->startTimer( $dm->name, $dm->source, $dm->category );
  49. }
  50. else if ( $dm->isStopTimer() )
  51. {
  52. $ezcDebug->getInstance()->stopTimer( $dm->name );
  53. }
  54. }
  55. else
  56. {
  57. // E_USER_WARNING, E_USER_ERROR
  58. $m = new ezcMessage( $msg );
  59. $m->setDefaultSource( "Paynet" );
  60. // Severity is translated: E_USER_WARNING => WARNING, E_USER_ERROR => ERROR.
  61. ezcLog::getInstance()->log( $m->message, $m->severity, $m->source, $m->category,
  62. array( "file" => $file, "line" => $line ) );
  63. }
  64. }
  65. // Set the errorHandler.
  66. set_error_handler( "errorHandler" );
  67. ////////////// Possible Debug Log messages: // ////////////////////////////////
  68. // [ Log, source, category ] Verbosity: Msg
  69. trigger_error( "[Log, Paynet, Templates ] 1: Loading header template." );
  70. // [ source, category ] Verbosity: Msg
  71. trigger_error( "[Paynet, Templates ] 2: Loading main template." );
  72. // [ category ] Verbosity: Msg
  73. // Source is specified in the error_handler
  74. trigger_error( "[ SQL ] 1: Last query: $query" );
  75. // [ category ] Verbosity: Msg
  76. // Use the default verbosity
  77. trigger_error( "[ SQL ] Last query: $query" );
  78. // Default category and verbosity
  79. trigger_error( "5 + 5 = 10" );
  80. // Timers:
  81. // [Timer_start, Src, Group] name
  82. trigger_error( "[ TIMER_START, Paynet, template_timers ] Loading header template." );
  83. // [Timer_start, Group/Category] name
  84. trigger_error( "[ TIMER_START, sql_timers ] Start_transaction" );
  85. // [Timer_stop] name
  86. trigger_error( "[ TIMER_STOP ] Start_transaction" );
  87. trigger_error( "[ TIMER_STOP ] Loading header template." );
  88. ////////////// Log messages: // ////////////////////////////////
  89. // Warnings and Errors are the same as Debug, except for the severity:
  90. // [ Src, Category ] msg
  91. trigger_error( "[paynet, template] Couldn't load the template: Header", E_USER_WARNING );
  92. // Example error:
  93. trigger_error( "[template] Couldn't produce any output", E_USER_ERROR );
  94. ////////////// Audit trails: // ////////////////////////////////
  95. ezcLog::getInstance()->log( "Added new user: $user", ezcLog::SUCCES_AUDIT, "paynet", "users" );
  96. ezcLog::getInstance()->log( "Couldn't delete user: $user", ezcLog::FAILED_AUDIT, "paynet", "users" );
  97. ////////////// Print debug output: // ////////////////////////////////
  98. print ( ezcDebug::getInstance()->getOutput() );
  99. ?>