1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2015 Red Hat Inc
8 * Author: John Spray <john.spray@redhat.com>
10 * This is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1, as published by the Free Software
13 * Foundation. See file COPYING.
17 #ifndef PY_FORMATTER_H_
18 #define PY_FORMATTER_H_
20 // Python.h comes first because otherwise it clobbers ceph's assert
22 // Python's pyconfig-64.h conflicts with ceph's acconfig.h
23 #undef HAVE_SYS_WAIT_H
26 #undef _POSIX_C_SOURCE
33 #include "common/Formatter.h"
34 #include "include/assert.h"
36 class PyFormatter : public ceph::Formatter
39 PyFormatter(bool pretty = false, bool array = false)
41 // It is forbidden to instantiate me outside of the GIL,
42 // because I construct python objects right away
44 // Initialise cursor to an empty dict
46 root = cursor = PyDict_New();
48 root = cursor = PyList_New(0);
52 ~PyFormatter() override
59 // Obscure, don't care.
60 void open_array_section_in_ns(const char *name, const char *ns) override
62 void open_object_section_in_ns(const char *name, const char *ns) override
67 const bool array = PyList_Check(root);
70 root = cursor = PyList_New(0);
72 root = cursor = PyDict_New();
76 void set_status(int status, const char* status_name) override {}
77 void output_header() override {};
78 void output_footer() override {};
79 void enable_line_break() override {};
81 void open_array_section(const char *name) override;
82 void open_object_section(const char *name) override;
83 void close_section() override
85 assert(cursor != root);
86 assert(!stack.empty());
90 void dump_bool(const char *name, bool b) override;
91 void dump_unsigned(const char *name, uint64_t u) override;
92 void dump_int(const char *name, int64_t u) override;
93 void dump_float(const char *name, double d) override;
94 void dump_string(const char *name, const std::string& s) override;
95 std::ostream& dump_stream(const char *name) override;
96 void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
98 void flush(std::ostream& os) override
100 // This class is not a serializer: this doens't make sense
104 int get_len() const override
106 // This class is not a serializer: this doens't make sense
111 void write_raw_data(const char *data) override
113 // This class is not a serializer: this doens't make sense
119 finish_pending_streams();
125 void finish_pending_streams();
130 std::stack<PyObject *> stack;
132 void dump_pyobject(const char *name, PyObject *p);
134 class PendingStream {
138 std::stringstream stream;
141 std::list<std::shared_ptr<PendingStream> > pending_streams;