OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. |
| 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. |
| 14 # |
| 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
| 18 from errno import EEXIST |
| 19 import json, tempfile, time, os |
| 20 |
| 21 def show_error(message, start_response, status="400 Processing Error"): |
| 22 start_response(status, [("Content-Type", "text/plain; charset=utf-8")]) |
| 23 return [message.encode("utf-8")] |
| 24 |
| 25 def log_filterhits(data, basepath, query_string): |
| 26 """ |
| 27 This logs the provided filterhits data as JSON to a file named after |
| 28 the current timestamp in a directory named after the current date. |
| 29 """ |
| 30 now = time.gmtime() |
| 31 |
| 32 dir_name = time.strftime("%Y-%m-%d", now) |
| 33 path = os.path.join(basepath, dir_name) |
| 34 try: |
| 35 os.makedirs(path) |
| 36 except OSError as e: |
| 37 if e.errno != EEXIST: |
| 38 raise |
| 39 |
| 40 with tempfile.NamedTemporaryFile( |
| 41 prefix = str(int(time.mktime(now))) + "-", |
| 42 suffix = ".log", |
| 43 dir = path, |
| 44 delete = False |
| 45 ) as f: |
| 46 f.write("[%s] \"%s\" %s\n" % (time.strftime('%d/%b/%Y:%H:%M:%S', now), |
| 47 query_string, json.dumps(data))) |
| 48 return f.name |
OLD | NEW |