Ϫf dZddlmZmZddlmZmZmZmZm Z m Z m Z m Z ddl mZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZeZeeefZd edefdZ dedefdZ!dedddfdede e!fgZ"e"Dcic] \}}}}|| c}}}}Z#dede$fdZ%de$defdZ&dedefdZ'dedefdZ( d'd eed!edefd"Z) d(d$eed!e ed%e*de efd&Z+y#cc}}}}w))zA Tools for saving and loading log events in a structured format. )dumpsloads)IOAnyAnyStrDictIterableOptionalUnioncast)UUID) NamedConstant)FailureFileLogObserver) flattenEvent)LogEvent)LogLevel)Loggerfailurereturnct|jt|jj|jjS)z Convert a failure to a JSON-serializable data structure. @param failure: A failure to serialize. @return: a mapping of strings to ... stuff, mostly reminiscent of L{Failure.__getstate__} ) __module____name__)type)dict __getstate__rrr)rs 6/usr/lib/python3/dist-packages/twisted/logger/_json.py failureAsJSONr s?  ||..\\**   failureDictcvtjt}|d}t|dd||d<||_|S)z Load a L{Failure} from a dictionary deserialized from JSON. @param failureDict: a JSON-deserialized object like one previously returned by L{failureAsJSON}. @return: L{Failure} rr)r__new__r__dict__)r"ftypeInfos rfailureFromJSONr).s@  A6"Hx 3RBKAJ Hr!c`t|txrtt|jd|uSN) isinstancergetattrrnamelevels rr1@s+ um , =%**d3u<r!z$02E59486-F24D-46AD-8224-3ACDF2A5732Ac.t|jS)N)r.)rr.r/s rr1r1Esd +r!c*tt|ddS)Nr.)r-rr/s rr1r1Fsghf t<r!c"t|tSr+)r,r)os rr1r1Is *Q(r!z$E76887E2-20ED-49BF-A8F8-BA25CC586F2DaDictcBd|vrtt|d|S|S)a# Dictionary-to-object-translation hook for certain value types used within the logging system. @see: the C{object_hook} parameter to L{json.load} @param aDict: A dictionary loaded from a JSON object. @return: C{aDict} itself, or the object represented by C{aDict} __class_uuid__) uuidToLoaderr )r6s robjectLoadHookr:Ts,5 D'7!89:5AA Lr! pythonObjectcntD])\}}}}||s||}t||d<|cSddiS)a Object-to-serializable hook for certain value types used within the logging system. @see: the C{default} parameter to L{json.dump} @param pythonObject: Any object. @return: If the object is one of the special types the logging system supports, a specially-formatted dictionary; otherwise, a marker dictionary indicating that it could not be serialized. r8 unpersistableT) classInfostr)r; predicateuuidsaverloaderresults robjectSaveHookrEdsO+4& 4 \ "<(F'*4yF# $M  T ""r!eventcndtdtttffd}t |t ||dS)a Encode an event as JSON, flattening it if necessary to preserve as much structure as possible. Not all structure from the log event will be preserved when it is serialized. @param event: A log event dictionary. @return: A string of the serialized JSON; note that this will contain no newline characters, and may thus safely be stored in a line-delimited file. unencodablercZt|tr|jdSt|S)z Serialize an object not otherwise serializable by L{dumps}. @param unencodable: An unencodable object. @return: C{unencodable}, serialized charmap)r,bytesdecoderE)rHs rdefaultzeventAsJSON..defaults* k5 )%%i0 0k**r!T)rMskipkeys)objectr JSONDictr?rr)rFrMs r eventAsJSONrQys7 +V +hm(< + $ 77r! eventTextc@ttt|tS)z Decode a log event from JSON. @param eventText: The output of a previous call to L{eventAsJSON} @return: A reconstructed version of the log event. ) object_hook)r rPrr:)rRs r eventFromJSONrUs % ~F GGr!outFilerecordSeparatorc"t|fdS)a Create a L{FileLogObserver} that emits JSON-serialized events to a specified (writable) file-like object. Events are written in the following form:: RS + JSON + NL C{JSON} is the serialized event, which is JSON text. C{NL} is a newline (C{"\n"}). C{RS} is a record separator. By default, this is a single RS character (C{"\x1e"}), which makes the default output conform to the IETF draft document "draft-ietf-json-text-sequence-13". @param outFile: A file-like object. Ideally one should be passed which accepts L{str} data. Otherwise, UTF-8 L{bytes} will be used. @param recordSeparator: The record separator to use. @return: A file log observer. c$t|dS)N )rQ)rFrWs rr1z%jsonFileLogObserver..s/!2;u3E2FbIr!r)rVrWs `rjsonFileLogObserverr[s, I r!NinFile bufferSizec# Kdtdtfd}dtdttfd |"||j d}|dk(r|}n d }n ||}d }|d k(rd } }ndtdttf fd }t|} |j |}|st |d kDr||} | | y|||z }|j|} | dd D] } t | d kDs|| } | | "| d }zw)aM Load events from a file previously saved with L{jsonFileLogObserver}. Event records that are truncated or otherwise unreadable are ignored. @param inFile: A (readable) file-like object. Data read from C{inFile} should be L{str} or UTF-8 L{bytes}. @param recordSeparator: The expected record separator. If L{None}, attempt to automatically detect the record separator from one of C{"\x1e"} or C{""}. @param bufferSize: The size of the read buffer used while reading from C{inFile}. @return: Log events as read from C{inFile}. srcHt|tr|S|jdS)Nutf-8)r,rKencode)r_s rasBytesz&eventsFromJSONLogFile..asBytess a H88G$ $r!recordc  t|jd} t |S#t$r#tj dt|YywxYw#t $r#tj dt|YywxYw)Nraz2Unable to decode UTF-8 for JSON record: {record!r}rdz&Unable to read JSON record: {record!r})rKrLUnicodeDecodeErrorlogerrorrU ValueError)rdtexts reventFromBytearrayz1eventsFromJSONLogFile..eventFromBytearrays =''0D  & &"  IIDV}      II>uV}I U s ( A)AA)BBNrr! cx|dtdk(r|Stjdt|y)NrZz0Unable to read truncated JSON record: {record!r}rf)ordrhrirK)rdrls reventFromRecordz.eventsFromJSONLogFile..eventFromRecords>bzSY&)&11 F =r!rrp)rrK bytearrayr rreadlensplit) r\rWr]rcfirstrecordSeparatorBytesrrbuffernewDatarFrecordsrdrls @reventsFromJSONLogFiler|sE(%6%e% 9(1C  A' G #( $'  '7s"$, I (82D u F ++j)6{Q'/$K ''"",,34crl F6{Q'/$K  ' sC'D+ D6 D))Ni),__doc__jsonrrtypingrrrrr r r r rAr constantlyrtwisted.python.failurer_filer_flattenr _interfacesr_levelsr_loggerrrhr?rPr r)r>r9rOr:rErQrUr[intr|)r@rArBrCs0000rrs III$*""! h S>7x$    g  $  34+< ) 34   &FOOO!A)T5&f O  ( v  ##H#*8x8C8>HSHXH.4 W'*:&*_ sG_c]__h _[PsC-