diff --git a/server/dup.go b/server/dup.go new file mode 100644 index 000000000..63d871054 --- /dev/null +++ b/server/dup.go @@ -0,0 +1,26 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !arm64 + +package server + +import ( + "syscall" +) + +// dup is an alias for syscall.Dup2 on most platforms or syscall.Dup3 on ARM64 +func (m *Command) dup(oldfd int, newfd int) error { + return syscall.Dup2(oldfd, newfd) +} diff --git a/server/dup_arm64.go b/server/dup_arm64.go new file mode 100644 index 000000000..726890fec --- /dev/null +++ b/server/dup_arm64.go @@ -0,0 +1,24 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package server + +import ( + "syscall" +) + +// dup is an alias for syscall.Dup2 on most platforms or syscall.Dup3 on ARM +func (m *Command) dup(oldfd int, newfd int) error { + return syscall.Dup3(oldfd, newfd, 0) +} diff --git a/server/server.go b/server/server.go index 80fccf4f9..a65ba09be 100644 --- a/server/server.go +++ b/server/server.go @@ -414,6 +414,30 @@ func (m *Command) setupNetworking() error { return errors.Wrap(gossipMemberSet.Open(), "opening gossip memberset") } +// setupLogger sets up the logger based on the configuration. +func (m *Command) setupLogger() error { + if m.Config.LogPath == "" { + m.logOutput = m.Stderr + } else { + f, err := os.OpenFile(m.Config.LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) + if err != nil { + return errors.Wrap(err, "opening file") + } + m.logOutput = f + err = m.dup(int(f.Fd()), int(os.Stderr.Fd())) + if err != nil { + return errors.Wrap(err, "syscall dup stderr to logfile") + } + } + + if m.Config.Verbose { + m.logger = logger.NewVerboseLogger(m.logOutput) + } else { + m.logger = logger.NewStandardLogger(m.logOutput) + } + return nil +} + // GossipTransport allows a caller to return the gossip transport created when // setting up the GossipMemberSet. This is useful if one needs to determine the // allocated ephemeral port programmatically. (usually used in tests) diff --git a/server/setup_logger.go b/server/setup_logger.go deleted file mode 100644 index 3dd0ff849..000000000 --- a/server/setup_logger.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2017 Pilosa Corp. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build !arm64 - -package server - -import ( - "os" - "syscall" - - "github.com/pilosa/pilosa/v2/logger" - "github.com/pkg/errors" -) - -// setupLogger sets up the logger based on the configuration. -func (m *Command) setupLogger() error { - if m.Config.LogPath == "" { - m.logOutput = m.Stderr - } else { - f, err := os.OpenFile(m.Config.LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) - if err != nil { - return errors.Wrap(err, "opening file") - } - m.logOutput = f - err = syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd())) - if err != nil { - return errors.Wrap(err, "dup2ing stderr onto logfile") - } - } - - if m.Config.Verbose { - m.logger = logger.NewVerboseLogger(m.logOutput) - } else { - m.logger = logger.NewStandardLogger(m.logOutput) - } - return nil -} diff --git a/server/setup_logger_arm64.go b/server/setup_logger_arm64.go deleted file mode 100644 index 4a0591582..000000000 --- a/server/setup_logger_arm64.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2017 Pilosa Corp. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package server - -import ( - "os" - "syscall" - - "github.com/pilosa/pilosa/v2/logger" - "github.com/pkg/errors" -) - -// setupLogger sets up the logger based on the configuration. -func (m *Command) setupLogger() error { - if m.Config.LogPath == "" { - m.logOutput = m.Stderr - } else { - f, err := os.OpenFile(m.Config.LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) - if err != nil { - return errors.Wrap(err, "opening file") - } - m.logOutput = f - err = syscall.Dup3(int(f.Fd()), int(os.Stderr.Fd()), 0) - if err != nil { - return errors.Wrap(err, "dup2ing stderr onto logfile") - } - } - - if m.Config.Verbose { - m.logger = logger.NewVerboseLogger(m.logOutput) - } else { - m.logger = logger.NewStandardLogger(m.logOutput) - } - return nil -}