-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.clj
117 lines (87 loc) · 2.76 KB
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(ns com.brunobonacci.mulog.utils
(:require [clojure.string :as str]
[clojure.pprint :as pp]
[clojure.walk :as w]))
(defn java-version
"It returns the current Java major version as a number"
[]
(as-> (System/getProperty "java.version") $
(str/split $ #"\.")
(if (= "1" (first $)) (second $) (first $))
(Integer/parseInt $)))
(defmacro os-java-pid
"it returns the OS pid for the current java process."
[]
(if (>= (java-version) 9)
`(.pid (java.lang.ProcessHandle/current))
;; java <= 8
`(-> (java.lang.management.ManagementFactory/getRuntimeMXBean)
(.getName)
(str/split #"@")
(first)
(Long/parseLong))))
(defn uuid
"It returns a random UUID as string"
[]
(str (java.util.UUID/randomUUID)))
(defn random-uid
"It returns a random 128-bit unique id with a base 36 encoding"
[]
(let [uuid (java.util.UUID/randomUUID)
u1 (.getMostSignificantBits uuid)
u2 (.getLeastSignificantBits uuid)
u1 (if (neg? u1) (- u1) u1)
u2 (if (neg? u2) (- u2) u2)]
(str (Long/toString u1 36)
(Long/toString u2 36))))
(defn puid
"It returns a random 128-bit unique id"
[]
(random-uid))
(defn edn-str
"Return a EDN representation of the given value.
Same as `pr-str` but without ellipsis."
[v & {:keys [pretty?] :or {pretty? false}}]
(binding [*print-length* nil
*print-level* nil]
(if pretty?
;; pretty-printed representation
(with-out-str
(pp/pprint v))
;; compact representation
(pr-str v))))
(defn pprint-event-str
"pretty print event to a string"
[m]
(let [top [:mulog/event-name :mulog/timestamp]
tops (set top)
mks (->> (keys m) (filter #(= "mulog" (namespace %))) (remove tops) (sort))
oks (->> (keys m) (remove #(= "mulog" (namespace %))) (sort))
get-value (fn [k] (get m k))]
(->> (mapcat (juxt identity get-value) (concat top mks oks))
(apply array-map)
(#(edn-str % :pretty? true)))))
(defn pprint-event
"pretty print event"
[m]
(println (pprint-event-str m)))
(defn exception-stacktrace
"returns a string representation of an exception and its stack-trace"
[^Throwable x]
(let [sw (java.io.StringWriter.)
pw (java.io.PrintWriter. sw)]
(.printStackTrace x ^java.io.PrintWriter pw)
(str sw)))
(defn remove-nils
"recursively remove nils from maps, vectors and lists."
[m]
(->> m
(w/postwalk
(fn [i]
(cond
(map? i) (into {} (remove (comp nil? second) i))
(map-entry? i) i
(vector? i) (into [] (remove nil? i))
(set? i) (into #{} (remove nil? i))
(sequential? i) (remove nil? i)
:else i)))))