Update RMR library Gerrit repo
[ric-plt/appmgr.git] / src / logger.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package main
21 /*
22 #cgo CFLAGS: -I/usr/local/include
23 #cgo LDFLAGS: -lmdclog
24 #
25 #include <mdclog/mdclog.h>
26 void xAppMgr_mdclog_write(mdclog_severity_t severity, const char *msg) {
27      mdclog_write(severity, "%s", msg);
28 }
29 */
30 import "C"
31
32 import (
33     "net/http"
34     "time"
35     "fmt"
36 )
37
38 func mdclog(severity C.mdclog_severity_t, msg string) {
39     msg = fmt.Sprintf("%s:: %s ", time.Now().Format("2019-01-02 15:04:05"), msg)
40
41     C.mdclog_mdc_add(C.CString("XM"), C.CString("1.0.0"))
42     C.xAppMgr_mdclog_write(severity, C.CString(msg))
43 }
44
45 func Logger(inner http.Handler) http.Handler {
46     return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47         inner.ServeHTTP(w, r)
48         s := fmt.Sprintf("Logger: method=%s url=%s", r.Method, r.URL.RequestURI())
49         mdclog(C.MDCLOG_DEBUG, s)
50     })
51 }