FileTarget.as 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.air.logging
  28. {
  29. import flash.filesystem.File;
  30. import flash.filesystem.FileMode;
  31. import flash.filesystem.FileStream;
  32. import mx.core.mx_internal;
  33. import mx.logging.targets.LineFormattedTarget;
  34. use namespace mx_internal;
  35. /**
  36. * An Adobe AIR only class that provides a log target for the Flex logging
  37. * framework, that logs files to a file on the user's system.
  38. *
  39. * This class will only work when running within Adobe AIR>
  40. */
  41. public class FileTarget extends LineFormattedTarget
  42. {
  43. private const DEFAULT_LOG_PATH:String = "app-storage:/application.log";
  44. private var log:File;
  45. public function FileTarget(logFile:File = null)
  46. {
  47. if(logFile != null)
  48. {
  49. log = logFile;
  50. }
  51. else
  52. {
  53. log = new File(DEFAULT_LOG_PATH);
  54. }
  55. }
  56. public function get logURI():String
  57. {
  58. return log.url;
  59. }
  60. mx_internal override function internalLog(message:String):void
  61. {
  62. write(message);
  63. }
  64. private function write(msg:String):void
  65. {
  66. var fs:FileStream = new FileStream();
  67. fs.open(log, FileMode.APPEND);
  68. fs.writeUTFBytes(msg + File.lineEnding);
  69. fs.close();
  70. }
  71. public function clear():void
  72. {
  73. var fs:FileStream = new FileStream();
  74. fs.open(log, FileMode.WRITE);
  75. fs.writeUTFBytes("");
  76. fs.close();
  77. }
  78. }
  79. }